C# 自定义属性在propertyGrid控件中显示

简介: 在上篇文章(地址: C# 设计时动态改变实体在PropertyGrid中显示出来的属性)中可以看到:自定义属性的显示是有问题的,那么如何修改呢?代码如下:public class PropertyDisplayConverterr<T> : ExpandableObjectConverter where T : IDisplay { public

在上篇文章(地址: C# 设计时动态改变实体在PropertyGrid中显示出来的属性)中可以看到:


自定义属性的显示是有问题的,那么如何修改呢?

代码如下:

public class PropertyDisplayConverterr<T> : ExpandableObjectConverter where T : IDisplay
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
        {
            if (destinationType == typeof(T))
                return true;
            return base.CanConvertTo(context, destinationType);
        }
        // This is a special type converter which will be associated with the T class.
        // It converts an T object to string representation for use in a property grid.
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
        {
            if (destinationType == typeof(System.String) && value is T)
            {
                return ((IDisplay)value).GetDisplayString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
接口:

 public interface IDisplay
    {
        /// <summary>
        /// 得到显示字符串
        /// </summary>
        /// <returns></returns>
        string GetDisplayString();
    }
修改上文中实体类如下:

 [TypeConverterAttribute(typeof(PropertyDisplayConverterr<IdentityColumnEntity>))]
    public class IdentityColumnEntity : IDisplay
    {
        private bool isIncrementColumn;
        /// <summary>
        /// 是否是自增列
        /// </summary>
        [Browsable(true)]
        [Category("基本")]
        [DisplayName("是否是自增列")]
        [ReadOnly(false)]
        [DefaultValue(false)]
        public bool IsIncrementColumn
        {
            set { isIncrementColumn = value; }
            get { return isIncrementColumn; }
        }

        private Int64 identityIncrement;
        /// <summary>
        /// 标识增量
        /// </summary>
        [Browsable(true)]
        [Category("基本")]
        [DisplayName("标识增量")]
        [ReadOnly(false)]
        [Description("标识增量属性指定在 Microsoft SQL Server 为插入的行生成标识值时,在现有的最大行标识值基础上所加的值。标识增量必须是 非零 整数,位数等于或小于 10。")]
        public Int64 IdentityIncrement
        {
            set { identityIncrement = value; }
            get { return identityIncrement; }
        }

        private Int64 ident_Seed;
        /// <summary>
        /// 标识种子 
        /// </summary>
        [Browsable(true)]
        [Category("基本")]
        [DisplayName("标识种子")]
        [ReadOnly(false)]
        [Description("指示标识列的初始行值。标识种子必须是  整数,位数等于或小于 10。")]
        public Int64 Ident_Seed
        {
            set { ident_Seed = value; }
            get { return ident_Seed; }
        }

        public string GetDisplayString()
        {
            if (this == null || this.IdentityIncrement == 0)
            {
                return "未设置自增列信息";
            }
            return String.Format("标识种子:{0};标识增量:{1}", this.Ident_Seed, this.IdentityIncrement);
        }
    }
效果如下:


演示代码:点击打开链接

本文参考:Customized display of collection data in a PropertyGrid

参考文章中demo:点击打开链接


拓展:

C# where泛型约束

类型参数的约束(C# 编程指南)



相关文章
|
1月前
|
C# 数据库 开发者
44.c#:combobox控件
44.c#:combobox控件
14 1
|
1月前
|
C# 数据库
40.c#:TreeView 控件
40.c#:TreeView 控件
14 1
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
36 0
|
29天前
|
C#
C#学习相关系列之自定义遍历器
C#学习相关系列之自定义遍历器
|
1月前
|
C# Windows
49.c#:StatusStrip 控件
49.c#:StatusStrip 控件
21 1
49.c#:StatusStrip 控件
|
1月前
|
C# 开发者 Windows
48.c#:toolstrip控件
48.c#:toolstrip控件
14 1
|
1月前
|
C# Windows
47.c#:menustrip控件
47.c#:menustrip控件
12 1
|
1月前
|
存储 缓存 C#
46.c#:datagridview控件
46.c#:datagridview控件
16 1
|
1月前
|
C#
45.c#:listview控件
45.c#:listview控件
10 1
|
1月前
|
C# 数据库 虚拟化
43.c#:listbox控件
43.c#:listbox控件
13 1