将枚举值作为命令参数从XAML传递

我想在WPF中传递一个枚举值作为命令参数,使用如下所示:

<Button
x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
CommandParameter="SearchPageType.First"
Content="Search">
</Button>

SearchPageType是一个枚举,这是为了了解从哪个按钮调用搜索命令。

这在WPF中是否可行,或者如何将枚举值作为命令参数传递?

69031 次浏览

Try this

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

local - is your namespace reference in the XAML

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>

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

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

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>

Try thisenter image description here

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