我在WPF项目中使用属性网格来显示 UI 项。所以基本上,对于集合类型的属性,需要一个组合框。为此,我使用了扩展的 PropertyValueEditor。问题在于,对于每个集合[比如语言或操作系统或任何其他集合...],我必须创建一个单独的组合框编辑器,因为每个集合的 ItemsSource 都不同。有没有办法创建一个通用的组合框并根据集合类型设置它的ItemsSource属性。(假设有一组特定的函数返回语言或操作系统的集合)
PS:我有大量的收藏品要展示。任何其他方法也将不胜感激!
请在下面找到示例代码:
这是操作系统集合的编辑器
public class OperatingSystemsEditor : ExtendedPropertyValueEditor
{
public OperatingSystemsEditor()
{
DataTemplate customTemplate = new DataTemplate();
FrameworkElementFactory comboBox = new FrameworkElementFactory(typeof (ComboBox));
LanguageParser utilObj = new LanguageParser();
comboBox.SetValue(ComboBox.ItemsSourceProperty, utilObj.GetOperatingSystemsAll());
Binding selectedItem = new Binding("Value");
comboBox.SetValue(ComboBox.SelectedItemProperty, selectedItem);
selectedItem.Mode = BindingMode.TwoWay;
this.InlineEditorTemplate = customTemplate;
this.InlineEditorTemplate.VisualTree = comboBox;
}
}
这是语言集合的编辑器
public class LanguagesEditor : ExtendedPropertyValueEditor
{
public LanguagesEditor()
{
DataTemplate customTemplate = new DataTemplate();
FrameworkElementFactory comboBox = new FrameworkElementFactory(typeof(ComboBox));
LanguageParser utilObj = new LanguageParser();
comboBox.SetValue(ComboBox.ItemsSourceProperty, utilObj.GetLanguagesAll());
Binding selectedItems = new Binding("Value");
comboBox.SetValue(ComboBox.SelectedItemProperty, selectedItems);
selectedItems.Mode = BindingMode.TwoWay;
this.InlineEditorTemplate = customTemplate;
this.InlineEditorTemplate.VisualTree = comboBox;
}
}
我如何使用这些编辑器:
[Editor(typeof(LanguagesEditor), typeof(ExtendedPropertyValueEditor))]
public ObservableCollection<>Languages
{get; set;}
[Editor(typeof(OperatingSystemsEditor), typeof(ExtendedPropertyValueEditor))]
public ObservableCollection<> OperatingSystems
{get; set;}