在上篇文章(地址: C# 設計時動態改變實體在PropertyGrid中顯示出來的屬性)中可以看到:
自定義屬性的顯示是有問題的,那麼如何修改呢?
代碼如下:
public class PropertyDisplayConverterr接口:: 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 { ///修改上文中實體類如下:/// 得到顯示字符串 /// ///string GetDisplayString(); }
[TypeConverterAttribute(typeof(PropertyDisplayConverterr效果如下:))] public class IdentityColumnEntity : IDisplay { private bool isIncrementColumn; /// /// 是否是自增列 /// [Browsable(true)] [Category(基本)] [DisplayName(是否是自增列)] [ReadOnly(false)] [DefaultValue(false)] public bool IsIncrementColumn { set { isIncrementColumn = value; } get { return isIncrementColumn; } } private Int64 identityIncrement; ////// 標識增量 /// [Browsable(true)] [Category(基本)] [DisplayName(標識增量)] [ReadOnly(false)] [Description(標識增量屬性指定在 Microsoft SQL Server 為插入的行生成標識值時,在現有的最大行標識值基礎上所加的值。標識增量必須是 非零 整數,位數等於或小於 10。)] public Int64 IdentityIncrement { set { identityIncrement = value; } get { return identityIncrement; } } private Int64 ident_Seed; ////// 標識種子 /// [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); } }