找不到枚举类型在WPF静态引用 [英] Unable to find enum type for static reference in WPF

查看:528
本文介绍了找不到枚举类型在WPF静态引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图枚举绑定到WPF中的单选按钮(通过<一个启发href="http://stackoverflow.com/questions/397556/wpf-how-to-bind-radiobuttons-to-an-enum/2908885#2908885">this回答),但我很难找到的枚举类型转换器参数:

I'm trying to bind an enum to a radio button in WPF (Inspired by this answer), but I have trouble finding the enum type for the converter parameter:

枚举以下面的方式定义

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

        public MySelections CurrentSelection;

        ...

    }
}

我想现在要绑定的复选框,这样的(数据上下文被认为是正确的,值转换器实现的:)

I am trying to bind now the checkbox like this (The data context is assumed to be correct and the value converter implemented:)

<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:静态模式:Enums.MySelections.one} 它不断地引发错误的类型型号:枚举。 MySelections 找不到。

The problem lies with {x:Static models:Enums.MySelections.one} which constantly throws the error that the type models:Enums.MySelections could not be found.

我如何才能找到我的枚举类型?

How can I find my enum type?

推荐答案

您可以将您的类之外声明它:

You could declare it outside of your class:

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

    public  class Enums
    {
        public MySelections CurrentSelection;

和那么这XAML将工作:

And then this xaml will work:

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


借助 X:静态 标记有固定的语法:


The x:Static markup has the fixed syntax:

{X:静态   preFIX:typeName.staticMemberName}

{x:Static prefix:typeName.staticMemberName}

这篇关于找不到枚举类型在WPF静态引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆