【问题标题】:How can I delete the default display string "(Collection)" in a PropertyGrid?如何删除 PropertyGrid 中的默认显示字符串“(集合)”?
【发布时间】:2019-05-09 16:17:20
【问题描述】:

我的目标是通过自己的 UITypeEditor 替换 PropertyGrid 属性的显示值。 但我无法删除始终显示的默认显示字符串“(Collection)”。

我尝试了 e.Graphics.Clear 并用白色画笔在图形中绘图。但这行不通。这是我的代码:

public class MyUITypeEditor : UITypeEditor
{
    public override void PaintValue(PaintValueEventArgs e)
    {
        // Not working:
        //e.Graphics.Clear(Color.White);
        //using (SolidBrush brush = new SolidBrush(Color.White))
        //{
        //    e.Graphics.FillRectangle(brush, e.Bounds);
        //}

        e.Graphics.DrawString(
            "0|0, 10|10",
            new Font("Arial", 10f, FontStyle.Bold),
            new SolidBrush(Color.Black),
            new Point(0, 0));
    }

    public override bool GetPaintValueSupported(ITypeDescriptorContext context)
    {
        return true;
    }
}

【问题讨论】:

    标签: c# collections graphics propertygrid


    【解决方案1】:

    你要做的是定义一个新的TypeConverter元素并覆盖下面的方法:

    public class test_typeconverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
        object value, Type destinationType)
                => "Text requiered";
    
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => true;
    
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => false;
    }
    

    然后,您必须将此类型定义为要在属性网格中显示的集合的类型转换器,如下所示:

    [TypeConverter(typeof(test_typeconverter))]
    public List<int> Values { get; set; }
    

    【讨论】:

    • 这对你有用吗?
    • 是的,正是我想要的,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多