Passing an enum value as command parameter from XAML

66,962

Solution 1

Try this

<Button CommandParameter="{x:Static local:SearchPageType.First}" .../>

local - is your namespace reference in the XAML

Solution 2

Also remember that if your enum is inside another class you need to use the + operator.

<Button CommandParameter="{x:Static local:MyOuterType+SearchPageType.First}".../>

Solution 3

You can use property element syntax instead of attribute syntax for this:

<Button x:Name="uxSearchButton"
        Command="{Binding Path=SearchMembersCommand}"
        Content="Search">
    <Button.CommandParameter>
        <SearchPageType>First</SearchPageType>
    </Button.CommandParameter>
</Button>

Solution 4

Also if you want to provide a [Flags] enum you can use the property element syntax:

<Button>
  <Button.CommandParameter>
    <SearchPageType>First,Second</SearchPageType>
  <Button.CommandParameter>
</Button>
Share:
66,962

Related videos on Youtube

akjoshi
Author by

akjoshi

Dot net developer having expertise in WPF. http://weblogs.asp.net/akjoshi

Updated on March 14, 2022

Comments

  • akjoshi
    akjoshi about 2 years

    I want to pass an enum value as command parameter in WPF, using something like this:

    <Button 
        x:Name="uxSearchButton" 
        Command="{Binding Path=SearchMembersCommand}" 
        CommandParameter="SearchPageType.First"
        Content="Search">
    </Button>
    

    SearchPageType is an enum and this is to know from which button search command is invoked.

    Is this possible in WPF, or how can you pass an enum value as command parameter?

  • Vering
    Vering almost 4 years
    If I have an enum called "MyEnum" in this namespace my.namespace, how to define "local"?
  • Yury Schkatula
    Yury Schkatula over 2 years
    Enum name as a XAML tag? Wow, I could never suppose such syntax to work, however it does!
  • Karl Stephen
    Karl Stephen about 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review