Passing an enum value as command parameter from XAML
Asked Answered
R

5

210

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?

Rosa answered 11/12, 2008 at 15:23 Comment(0)
S
334

Try this

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

local - is your namespace reference in the XAML

Shakiashaking answered 11/12, 2008 at 16:55 Comment(0)
E
190

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

<Button CommandParameter="{x:Static local:MyOuterType+SearchPageType.First}".../>
Erickericka answered 4/12, 2010 at 5:26 Comment(0)
K
54

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>
Kwasi answered 11/12, 2008 at 19:48 Comment(1)
Enum name as a XAML tag? Wow, I could never suppose such syntax to work, however it does!Averyaveryl
S
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>
Sandon answered 22/8, 2014 at 5:38 Comment(0)
M
-1

Try thisenter image description here

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

Mobster answered 13/3, 2022 at 14:21 Comment(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 ReviewExpressive

© 2022 - 2024 — McMap. All rights reserved.