Sergey Aldoukhov
Sergey Aldoukhov

Reputation: 22744

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

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="{???}"/>

Upvotes: 3

Views: 1415

Answers (1)

itowlson
itowlson

Reputation: 74832

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}" />

Upvotes: 10

Related Questions