3

Here is how it looks like:

When changing markup extension Key property - everything works.

When changing markup extension constructor argument - it's not updated. Workaround is to update property with extension (change Text) and then back. Then value is evaluated correctly.

Here is extension:

public class MyExtension : MarkupExtension
{
    public string Key { get; set; }

    public MyExtension() { }

    public MyExtension(string key)
    {
        Key = key;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Key;
    }
}

Any ideas of how to make designer work with constructor argument same way as it does with property?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Which version of VS are you using? – Il Vic Jan 07 '16 at 09:34
  • @IlVic, didn't though it could be a VS bug. Using Microsoft Visual Studio Community 2015 Version 14.0.24720.00 Update 1. Tell me please if it works correctly for you. – Sinatr Jan 07 '16 at 09:40
  • You MarkupExtension works perfectly - for example - with Visual Studio 2010 Premium Edition (version 10.0.40219.1 SP1Rel). I can check with Visual Studio 2013 too, if you need. But I do not have Visual Studio 2015 – Il Vic Jan 07 '16 at 09:57
  • @IlVic, are you sure? Try to change in xaml `{local:My 1}` -> `{local:My 123}` is rendered in designer window updated properly after each change? `{local:My Key=123}` works, but without `Key` when I try to change it stays for me unchanged. I tried to disable extensions, but the problem still. – Sinatr Jan 07 '16 at 10:06
  • Yes, I am sure. I copied your `MyExtension` code then I built my solution. Then if I change `{local:My a}` in `{local:My abc}` then "abc" is immediately rendered in the XAML preview (or designer, as you prefer). – Il Vic Jan 07 '16 at 10:39
  • @IlVic, thanks. I am not sure what to do now. Looks like I am doomed to use extension with `Key`. – Sinatr Jan 07 '16 at 10:44
  • @Sinatr did you ever find a solution to this? I'm facing the same issue with VS 2019 – Markus Hütter Apr 21 '20 at 15:33
  • @MarkusHütter, nope. I am just using property. – Sinatr Apr 22 '20 at 06:52

1 Answers1

2

What seems to remedy this situation is the use of ConstructorArgumentAttribute like so:

public class MyExtension : MarkupExtension
{
    [ConstructorArgument("key")]
    public string Key { get; set; }

    public MyExtension() { }

    public MyExtension(string key)
    {
        Key = key;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Key;
    }
}
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63