将ComboBox绑定到嵌套在类中的枚举

Pet*_*r M 10 wpf enums combobox nested class

我一直在疯狂地将组合框绑定到类的枚举类型属性,其中枚举本身在同一个类中声明.

我试图按照这里提供的答案(wpf组合框绑定到枚举我做错了什么?)具体我使用建议的MarkupExtension代码和匹配的xaml代码.

我的工作代码是:

在单独的文件中定义枚举.

namespace EnumTest
{
    public enum TestEnum {one, two, three, four };
}
Run Code Online (Sandbox Code Playgroud)

使用Enum的类(请注意,已删除propertyChanged代码以简化操作):

namespace EnumTest
{
    public class Test : INotifyPropertyChanged
    {
        private TestEnum _MyVar;
        public TestEnum MyVar { 
            get { return _MyVar; } 
            set 
            { 
                _MyVar = value;
                OnPropertyChanged("MyVar");
            } 
        }

        public Test()
        {
            _MyVar = TestEnum.three;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用该类的程序文件:

namespace EnumTest
{
    public partial class Window1 : Window
    {
        Test _oTest = new Test();

        public Window1()
        {
            InitializeComponent();
            cmbBox.DataContext = _oTest;
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

显示枚举的扩展方法

namespace EnumTest
{
    [MarkupExtensionReturnType(typeof(object[]))]
    public class EnumValuesExtension : MarkupExtension
    {
        public EnumValuesExtension()
        {
        }

        public EnumValuesExtension(Type enumType)
        {
            this.EnumType = enumType;
        }

        [ConstructorArgument("enumType")]
        public Type EnumType { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (this.EnumType == null)
                throw new ArgumentException("The enum type is not set");
            return Enum.GetValues(this.EnumType);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以及用于显示数据的xaml代码:

<Window x:Class="EnumTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:w="clr-namespace:EnumTest"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ComboBox Name="cmbBox" 
                  Height="20" 
                  Width="80" 
                  ItemsSource="{Binding Source={w:EnumValues EnumType=w:TestEnum}}" 
                  SelectedItem="{Binding Path=MyVar}"
                  />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

以上都是好的和花花公子,但我想 Test类中定义Enum 并抛弃Enum在全局范围内的定义.像这样:

namespace EnumTest
{
    public class Test : INotifyPropertyChanged
    {
        // Declare Enum **INSIDE** the class
        public enum TestEnum {one, two, three, four };
        private TestEnum _MyVar;
        public TestEnum MyVar { 
            get { return _MyVar; } 
            set 
            { 
                _MyVar = value;
                OnPropertyChanged("MyVar");
            } 
        }

        public Test()
        {
            _MyVar = TestEnum.three;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我提到的SO问题暗示匹配的xaml语法为:

        <ComboBox Name="cmbBox" 
                  ...
                  ItemsSource="{Binding Source={w:EnumValues EnumType=w:Test+TestEnum}}" 
                  ...
                  />
Run Code Online (Sandbox Code Playgroud)

但这种(某种程度)对我不起作用.当我进行干净的构建时,我在VS 2008状态栏上收到"构建成功"消息,但我也在xaml中报告了错误

Type 'Test+TestEnum' was not found.  
Run Code Online (Sandbox Code Playgroud)

但代码按预期运行!

但是这意味着xaml设计器不会加载.所以我有点搞砸了更多的wpf工作,直到我能清除xaml错误.

我现在想知道这是否是VS 2008 SP1问题,而不是我的问题.

编辑

  1. 使我的问题陈述更明确.
  2. 试过Joel的第一个解决方案,但我最终得到了代码运行 2个xaml错误
  3. 尝试了乔尔的第二个解决方案,并且开箱即用 - 所以我要去那个!

注意 我从中得到MarkupExtension代码的SO问题对xaml使用了这种语法:

<ComboBox ItemsSource="{w:EnumValues w:TestEnum}"/>
Run Code Online (Sandbox Code Playgroud)

当我使用它时,我得到一个编译错误,说没有EnumValues构造函数接受1个参数.我做了一些谷歌搜索,这似乎是VS中的一个错误.我正在使用VS 2008 SP1.我确实看到一些评论暗示它出现在VS 2010测试版中.无论如何,这就是我使用xaml语法的原因

<ComboBox ItemsSource="{w:EnumValues EnumType=w:TestEnum}"/>
Run Code Online (Sandbox Code Playgroud)

因为这种语法有效!

Joe*_*ant 3

获取枚举值用作数据源的另一种方法:

<Window.Resources>
    <ObjectDataProvider
        MethodName="GetValues"
        ObjectType="{x:Type sys:Enum}"
        x:Key="TestValues">
        <ObjectDataProvider.MethodParameters>
            <w:Type2
                TypeName="w:Test+TestEnum" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

...

ItemsSource="{Binding Source={StaticResource TestValues}}"
Run Code Online (Sandbox Code Playgroud)

Type2Extension请注意,由于 和TypeExtension嵌套类型的怪异,您仍然需要。但您不需要额外的自定义标记扩展。如果您要在多个位置使用该列表,这种方法会更好,因为您可以在App.xaml资源中声明它。