C# 自定義屬性在propertyGrid控制元件中顯示

衣舞晨風發表於2015-11-21

在上篇文章(地址: 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# 程式設計指南)



相關文章