我正在使用带有数据绑定的Xceed PropertyGrid和AutoGenerateProperties = true。我有如下所述的可空属性导致奇怪的UI行为。
网格允许我单击值Yes和No,但null选项稍微被属性网格隐藏,并且不允许我单击它以选择它。如果我选择是并使用向上箭头键我可以选择它。 Microsoft属性网格已满显示空选项,并允许我单击它。
我做错了什么或这是一个错误?我在GitHub问题中问过,但对我的issue没有回应。
YesNo? _compressed;
[CategoryAttribute("Package")]
[Description("Set to 'yes' to have compressed files in the source. This attribute cannot be set for merge modules. ")]
public YesNo? Compressed { get { return _compressed; } set { _compressed = value; RaisePropertyChangedEvent("Compressed"); } }
这不是一个真正的错误。如果你想将default(YesNo?)
的值显示为空的string
或null
,你需要定义你想以某种方式显示它的方式。你可以通过创建自己的自定义编辑器来完成:
public class CustomEditor:Xceed.Wpf.Toolkit.PropertyGrid.Editors.ComboBoxEditor {
protected override IValueConverter CreateValueConverter()
{
return new CustomValueConverter<T>();
}
protected override ComboBox CreateEditor()
{
ComboBox comboBox = base.CreateEditor();
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetBinding(TextBlock.TextProperty, new Binding(".") { Converter = new CustomValueConverter<T>() });
comboBox.ItemTemplate = new DataTemplate() { VisualTree = textBlock };
return comboBox;
}
protected override IEnumerable CreateItemsSource(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
{
return new string[1] { CustomValueConverter<T>.Null }
.Concat(Enum.GetValues(typeof(T)).OfType<T>().Select(x => x.ToString()));
}
}
public class CustomValueConverter:IValueConverter {internal const string Null =“”; public object Convert(object value,System.Type targetType,object parameter,CultureInfo culture){if(value == null)return null;
return value.ToString();
}
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
{
string s = value?.ToString();
if (s == Null)
return null;
return Enum.Parse(typeof(T), s);
}
}
用法:
YesNo? _compressed;
[CategoryAttribute("Package")]
[Description("Set to 'yes' to have compressed files in the source. This attribute cannot be set for merge modules. ")]
[Editor(typeof(CustomEditor<YesNo>), typeof(CustomEditor<YesNo>))]
public YesNo? Compressed { get { return _compressed; } set { _compressed = value; RaisePropertyChangedEvent("Compressed"); } }