203

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?

akjoshi
  • 15,374
  • 13
  • 103
  • 121

5 Answers5

323

Try this

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

local - is your namespace reference in the XAML

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
186

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

<Button CommandParameter="{x:Static local:MyOuterType+SearchPageType.First}".../>
akjoshi
  • 15,374
  • 13
  • 103
  • 121
tbergelt
  • 2,196
  • 1
  • 14
  • 5
52

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>
Robert Macnee
  • 11,650
  • 4
  • 40
  • 35
32

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>
akjoshi
  • 15,374
  • 13
  • 103
  • 121
hartmape
  • 543
  • 5
  • 5
-1

Try thisenter image description here

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

Mhd Sheikh
  • 32
  • 3
  • 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](/review/late-answers/31278470) – Karl Stephen Mar 16 '22 at 12:30