我使用Xceed Extended WPF Toolkit在PropertyGrid
中显示[Flags]
属性的枚举。WPF Toolkit:CheckComboBox和[Flags] enum
[Flags]
public enum TestEnum
{
Test1 = 1,
Test2 = 2,
Test3 = 4,
Test4 = 8,
Test5 = 16,
Test6 = 32,
Test7 = 64,
}
因为我不知道在编译时枚举定义,我会动态地创建使用EnumBuilder枚举。
我创建了一个编辑器来显示枚举为CheckComboBox
:
public class CheckComboBoxEditor : TypeEditor<CheckComboBox>, ITypeEditor
{
protected override void SetValueDependencyProperty()
{
ValueProperty = CheckComboBox.SelectedValueProperty;
}
protected override CheckComboBox CreateEditor()
{
return new CheckComboBox();
}
protected override void ResolveValueBinding(PropertyItem propertyItem)
{
var _binding = new Binding("Value");
_binding.Source = propertyItem;
_binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
_binding.Mode = BindingMode.TwoWay;
_binding.Converter = CreateValueConverter();
BindingOperations.SetBinding(Editor, CheckComboBox.SelectedValueProperty, _binding);
var _binding2 = new Binding("Value");
_binding2.Source = propertyItem;
_binding2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
_binding2.Mode = BindingMode.TwoWay;
_binding2.Converter = CreateValueConverter();
BindingOperations.SetBinding(Editor, CheckComboBox.SelectedItemProperty, _binding2);
Editor.ItemsSource = Enum.GetValues(propertyItem.Value.GetType());
}
}
正如你所看到的,到目前为止,我一直在努力,每个SelectedValue
和SelectedItem
属性绑定。 CreateValueConverter()
在基类中定义并返回null
。
它运作良好,如果我选择框中的一些值,并打我的保存按钮 - 在我的模型中,我收到正确的枚举值。但它不工作在其他方向 - 如果我设置任何枚举值(带或不带标志)到我的属性,所有值都未选中,内容区域是空。
你有什么想法来解决这个问题吗?
什么我忘了说 - 对不起,我会更新我的问题 - 是我不能依靠一个编译时枚举。我会动态地使用'[EnumBuilder](https://msdn.microsoft.com/de-de/library/system.reflection.emit.enumbuilder%28v=vs.110%29.aspx)''创建'Enum'。 –
更新了代码以处理动态生成的带有flags属性的枚举。 –