.net 将枚举值作为 XAML 中的命令参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/359699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing an enum value as command parameter from XAML
提问by akjoshi
I want to pass an enum value as command parameter in WPF, using something like this:
我想在 WPF 中传递一个枚举值作为命令参数,使用如下所示:
<Button
x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
CommandParameter="SearchPageType.First"
Content="Search">
</Button>
SearchPageTypeis an enum and this is to know from which button search command is invoked.
SearchPageType是一个枚举,这是为了知道从哪个按钮搜索命令被调用。
Is this possible in WPF, or how can you pass an enum value as command parameter?
这在 WPF 中是否可行,或者如何将枚举值作为命令参数传递?
回答by Jobi Joy
Try this
尝试这个
<Button CommandParameter="{x:Static local:SearchPageType.First}" .../>
local- is your namespace referencein the XAML
local- 是您在 XAML 中的命名空间引用
回答by tbergelt
Also remember that if your enum is inside another class you need to use the +operator.
还要记住,如果您的枚举在另一个类中,您需要使用+运算符。
<Button CommandParameter="{x:Static local:MyOuterType+SearchPageType.First}".../>
回答by Robert Macnee
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>
回答by hartmape
Also if you want to provide a [Flags] enum you can use the property element syntax:
此外,如果您想提供 [ Flags] 枚举,您可以使用属性元素语法:
<Button>
<Button.CommandParameter>
<SearchPageType>First,Second</SearchPageType>
<Button.CommandParameter>
</Button>

