How to write a value object in XAML using markup extension?

wpf, xaml

Solution

You can write a markup extension by deriving from the MarkupExtension class and implementing the ProvideValue method:

public class BooleanValueExtension : MarkupExtension
{
  private readonly bool _value;

  public BooleanValueExtension(bool value)
  {
    _value = value;
  }

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

You can then use this using the brace syntax:

<Button CommandParameter="{local:BooleanValue True}" />

Problem

I want to replace ``` <Button Text="Foo" Command="{Binding Foo}"> <Button.CommandParameter> <System:Boolean>True</System:Boolean> </Button.CommandParameter> </Button> ``` with something like ``` <Button ... CommandParameter="{???}"/> ```

Original source