Passing an enum value as command parameter from XAML

.NetWpfSilverlightXamlCommand

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

Try this

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

local - is your namespace reference in the XAML

Solution 2 - .Net

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 - .Net

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 - .Net

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>

Solution 5 - .Net

Try thisenter image description here

CommandParameter="{x:Static "Class namespace e.g(Models)":SearchPageType.First}"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionakjoshiView Question on Stackoverflow
Solution 1 - .NetJobi JoyView Answer on Stackoverflow
Solution 2 - .NettbergeltView Answer on Stackoverflow
Solution 3 - .NetRobert MacneeView Answer on Stackoverflow
Solution 4 - .NethartmapeView Answer on Stackoverflow
Solution 5 - .NetMhd SheikhView Answer on Stackoverflow