C# 属性网格编号格式

C# 属性网格编号格式,c#,winforms,propertygrid,C#,Winforms,Propertygrid,是否可以格式化winforms的PropertyGrid中显示的数值属性 class MyData { public int MyProp {get; set;} } 例如,我希望它在网格中显示为1.000.000 有一些属性吗?您应该为integer属性实现自定义: class MyData { [TypeConverter(typeof(CustomNumberTypeConverter))] public int MyProp { get; set; } }

是否可以格式化winforms的PropertyGrid中显示的数值属性

class MyData
{
      public int MyProp {get; set;}
}
例如,我希望它在网格中显示为1.000.000


有一些属性吗?

您应该为integer属性实现自定义:

class MyData
{
    [TypeConverter(typeof(CustomNumberTypeConverter))]
    public int MyProp { get; set; }
}
PropertyGrid使用TypeConverter将对象类型(本例中为整数)转换为字符串,用于在网格中显示对象值。在编辑过程中,TypeConverter会将字符串转换回您的对象类型

因此,您需要使用类型转换器,它应该能够使用千个分隔符将整数转换为字符串,并将此类字符串解析回整数:

public class CustomNumberTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, 
                                        Type sourceType)
    {
        return sourceType == typeof(string);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, 
        CultureInfo culture, object value)
    {            
        if (value is string)
        {
            string s = (string)value;
            return Int32.Parse(s, NumberStyles.AllowThousands, culture);
        }

        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, 
        CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
            return ((int)value).ToString("N0", culture);

        return base.ConvertTo(context, culture, value, destinationType);
    }
}
结果:

propertyGrid.SelectedObject = new MyData { MyProp = 12345678 };

我建议你读书
MSDN文章,以了解PropertyGrid如何工作以及如何对其进行自定义。

我不知道如何直接在PropertyGrid中格式化属性,但您可以执行以下操作

class MyData
{
    [Browsable(false)]
    public int _MyProp { get; set; }

    [Browsable(true)]
    public string MyProp
    {
        get
        {
             return _MyProp.ToString("#,##0");
        }
        set
        {
             _MyProp = int.Parse(value.Replace(".", ""));
        }
    }
}

PropertyGrid中只显示了
可浏览(true)
属性。

我有同样的问题,并提出了一个比Sergy的答案更灵活的解决方案。它涉及类型转换器和自定义属性。TypeConverter负责执行转换,custom属性告诉TypeConverter您希望字符串的格式

我声明我的示例类如下:

class MyData
{
    [TypeConverter(typeof(FormattedDoubleConverter))]
    [FormattedDoubleFormatString("F3")]
    public double MyProp { get; set; }
}
class FormattedDoubleConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) || sourceType == typeof(double);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(string) || destinationType == typeof(double);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                       object value)
    {
        if (value is double)
            return value;

        var str = value as string;
        if (str != null)
            return double.Parse(str);

        return null;
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                     object value, Type destinationType)
    {
        if (destinationType != typeof(string))
            return null;

        if (value is double)
        {
            var property = context.PropertyDescriptor;
            if (property != null)
            {
                // Analyze the property for a second attribute that gives the format string
                var formatStrAttr = property.Attributes.OfType<FormattedDoubleFormatString>().FirstOrDefault();
                if (formatStrAttr != null)
                    return ((double)value).ToString(formatStrAttr.FormatString);
                else
                    return ((double)value).ToString();
            }
        }

        return null;
    }
}
类型转换器的实现如下所示:

class MyData
{
    [TypeConverter(typeof(FormattedDoubleConverter))]
    [FormattedDoubleFormatString("F3")]
    public double MyProp { get; set; }
}
class FormattedDoubleConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) || sourceType == typeof(double);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(string) || destinationType == typeof(double);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                       object value)
    {
        if (value is double)
            return value;

        var str = value as string;
        if (str != null)
            return double.Parse(str);

        return null;
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                     object value, Type destinationType)
    {
        if (destinationType != typeof(string))
            return null;

        if (value is double)
        {
            var property = context.PropertyDescriptor;
            if (property != null)
            {
                // Analyze the property for a second attribute that gives the format string
                var formatStrAttr = property.Attributes.OfType<FormattedDoubleFormatString>().FirstOrDefault();
                if (formatStrAttr != null)
                    return ((double)value).ToString(formatStrAttr.FormatString);
                else
                    return ((double)value).ToString();
            }
        }

        return null;
    }
}

就在这里。一种可用于任何格式的解决方案。您甚至可以通过将其更改为转换任何实现
I可转换
的类型,使其在某种程度上独立于类型,但我不打算深入讨论。

这看起来应该是三个属性,即主要属性、次要属性和修订属性。如果可能,您需要编写自己的格式设置程序,以便使用like String。FORMAT OP使用欧洲编号方案,千位分隔符为
,而不是
,与美国类似。因此,这个问题与
Version
格式无关,而是与数值格式有关。这实际上是我的首选解决方案,因为有可用的自定义级别。作为一个有趣的调整,参数
context.Instance
指向正在显示的对象,以及是否有任何格式线索(如格式字符串属性)exist您可以使用它们在
ToString()
中运行。请注意,此示例特定于int,不会处理long等。