Passing an enum value as command parameter from XAML

烈酒焚心 提交于 2019-12-17 04:44:13

问题


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?


回答1:


Try this

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

local - is your namespace reference in the XAML




回答2:


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

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



回答3:


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>



回答4:


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>


来源:https://stackoverflow.com/questions/359699/passing-an-enum-value-as-command-parameter-from-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!