16

我正在尝试将枚举绑定到 WPF 中的单选按钮(受​​此答案的启发),但我无法找到转换器参数的枚举类型:

枚举定义如下

namespace Application.Models
{
    public class Enums
    {
        public enum MySelections { one, two ,three };

        public MySelections CurrentSelection;

        ...

    }
}

我现在正在尝试绑定这样的复选框(假设数据上下文是正确的并且实现了值转换器:)

<Window x:Class="Application.MainWindow"
        ....
        xnlns:models="clr-namespace:Application.Models" >

...
<RadioButton Content="One"
             IsChecked="{Binding Path=CurrentSelection, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static models:Enums.MySelections.one}}" />
...

问题在于{x:Static models:Enums.MySelections.one}不断抛出models:Enums.MySelections找不到类型的错误。

如何找到我的枚举类型?

4

2 回答 2

48

使用“+”而不是“。” 在 XAML 中获取嵌套类型:

{x:Static models:Enums+MySelections.one}
于 2011-04-15T12:37:03.870 回答
17

你可以在你的班级之外声明它:

namespace Application.Models
{
    public enum MySelections { one, two, three };

    public  class Enums
    {
        public MySelections CurrentSelection;

然后这个 xaml 将起作用:

.... ConverterParameter={x:Static models:MySelections.one}

标记具有固定的x:Static语法:

{x:静态前缀:typeName.staticMemberName}

于 2011-04-15T12:30:02.933 回答