本文介绍了如何格式化/更改属性网格中的显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Winform Property Grid ,但是我无法格式化显示的值(注意与 wpf 紧密相关)现在)

Am trying to work with a Winform Property Grid, and I am not able to format displayed value (mind too strongly tied to wpf now)

所以我想要的是,属性网格中有一个下拉菜单,它有自己的 UITypeEditor ,此编辑器显示了诸如

So what I want is, there is a drop down in property grid that has its own UITypeEditor, this editor shows values such as

1 - On
2 - Off
3 - Unknown

所以侦听propertyGrid更改的属性是int,出于某些奇怪的原因,我无法将其更改为字符串,因此像在wpf中一样,我可以使用将 1转换为1- On的转换器之类的东西 1-On到1 吗?

so the property on that listens to propertyGrid changes is int and for some odd reasons I cant change it to string, so like in wpf can I have a converter sort of thing that converts 1 into 1- On and 1-On to 1 ?

如何装饰我的财产或财产网格以使其变得如此聪明?

how can I decorate my property or property grid to be this intelligent ?

我的财产看起来像

[LocalizedCategory("Limits", typeof(Api.Properties.Resources))]
[LocalizedDisplayName("Maximum", typeof(Api.Properties.Resources))]
[LocalizedDescription("Maximum", typeof(Api.Properties.Resources))]
[Editor(typeof(TextConversionTypeEditor), typeof(UITypeEditor))]
public int CriticalMaximum
{
    get; set;
}

我可以让我的属性网格显示更多信息,而不仅仅是一个int吗?

Can I make my property grid display more information than just an int ?

推荐答案

如果您可以使用 Enum 作为属性的类型,则它将在下拉列表中显示可用值,否则您可以创建 TypeConverter 为下拉列表提供值.为此,您可以使用以下任一选项:

If you can use an Enum as type of your property, then it shows available values in drop-down, otherwise you can create a TypeConverter to provide values for drop-down. To do so you can use either of these options:

为您的int属性使用枚举的TypeConverter

如果值是有限的,并且在设计时是已知的,在这种情况下,尽管属性为 int ,但是您可以为属性使用 Enum 的转换器,而无需覆盖任何内容:

If values are limited and known at design-time, In this case although the property is int, you can use the converter of an Enum for your property, without overriding anything:

public class MyObject
{
    [TypeConverter(typeof(MyTypeConverter))]
    public int MyProperty { get; set; }
}
public class MyTypeConverter : EnumConverter
{
    public MyTypeConverter() : base(typeof(MyValues)) { }
}
public enum MyValues
{
    On = 1,
    Off,
    Unknown
}

创建自己的支持标准值的TypeConverter

如果您没有枚举,并且在运行时生成了标准值,则可以创建这样的 TypeConverter :

If you can not have an enum and your standard values are generated at run-time, you can create such TypeConverter:

public class MyTypeConverter : TypeConverter
{
    Dictionary<int, string> values;
    public MyTypeConverter()
    {
        values = new Dictionary<int, string> { { 1, "1 - On" }, { 2, "2 - Off" }, { 3, "3 - Unknown" } };
    }
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string)) return true;
        return base.CanConvertFrom(context, sourceType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value != null && values.ContainsValue(value.ToString()))
            return values.Where(x => x.Value == value.ToString()).FirstOrDefault().Key;
        return base.ConvertFrom(context, culture, value);
    }
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string) && value != null && value.GetType() == typeof(int))
            return values[(int)value];
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(values.Keys);
    }
}

这篇关于如何格式化/更改属性网格中的显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 12:06