Search code examples
c#wpfmarkup-extensionsxaml-designer

MarkupExtension: changing constructor argument is not evaluated in design time


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?


Solution

  • 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;
        }
    }