我使用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屬性的枚舉。 –