我一直在疯狂地将组合框绑定到类的枚举类型属性,其中枚举本身在同一个类中声明.
我试图按照这里提供的答案(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)