213

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

340

Try this

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

local - is your namespace reference in the XAML

Sign up to request clarification or add additional context in comments.

Comments

193

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

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

Comments

56

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 Comment

Enum name as a XAML tag? Wow, I could never suppose such syntax to work, however it does!
34

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> 

Comments

-3

Try thisenter image description here

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.