我正在将Xceed PropertyGrid与数据绑定一起使用,并且AutoGenerateProperties = true。我有如下所述的可空属性,会导致奇怪的UI行为。
该网格允许我单击“是”和“否”,但是该属性网格稍微掩盖了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"); } }
答案 0 :(得分:1)
这实际上不是错误。如果要将default(YesNo?)
的值显示为除空白string
或null
之外的其他值,则需要定义希望其显示方式的方式。您可以通过创建自己的自定义编辑器来做到这一点:
公共类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()));
}
}
公共类CustomValueConverter:IValueConverter { 内部const字符串Null =“”; 公共对象Convert(对象值,System.Type targetType,对象参数,CultureInfo文化) { 如果(值==空) 返回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"); } }