201

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?

0

5 Answers 5

317

Try this

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

local - is your namespace reference in the XAML

0
184

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

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

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>
1
  • Enum name as a XAML tag? Wow, I could never suppose such syntax to work, however it does! Nov 23, 2021 at 17:24
33

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>
0
-1

Try thisenter image description here

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

1
  • 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 Mar 16, 2022 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.