PropertyGrid 綁定動態屬性與值的集合

來源:互聯網
上載者:User
文章目錄
  • 建立Class XProp
  • 建立Class XPropDescriptor
  • 建立Class XProps

建立下面3個類:

  • XProp
  • XPropDescriptor
  • XProps
建立Class XProp

XProp 就是使用者將具體使用的屬性。PropertyGrid 很炫的一點就是屬性的定製可以有很大的自由度。在這裡,給出一種最最簡單的形式,屬性包括 Name 和 Value 兩個部分,在 PropertyGrid 的屬性視窗中,Name 將代表左邊的屬性名稱,Value 將代表右邊的屬性值。

using System;   
using System.ComponentModel;   
using System.Collections.Generic;   
using System.Text;   
  
namespace CustomerPropertyGrid   
{   
    class XProp   
    {   
        private string theName = "";   
        private object theValue = null;   
  
        public string Name   
        {   
            get  
            {   
                return this.theName;   
            }   
            set  
            {   
                this.theName = value;   
            }   
        }   
        public object Value   
        {   
            get  
            {   
                return this.theValue;   
            }   
            set  
            {   
                this.theValue = value;   
            }   
        }   
  
        public override string ToString()   
        {   
            return "Name: " + Name + ", Value: " + Value;   
        }   
    }   
}  

這個Class沒有特別的地方,對 ToString 的 Overrides 是為了方便後面對整個類的內容顯示。

建立Class XPropDescriptor

XPropDescriptior 是建立在 XProp 基礎上的,同時是 PropertyDescriptor 的一個衍生類別。PropertyDescriptor是在 System.ComponentModel 中的,包含了 PropertyGrid 中每個屬性所對應的各種介面。XPropDescriptior 就是把 XProp 中的屬性和這些介面對應起來。

using System;   
using System.ComponentModel;   
using System.Collections.Generic;   
using System.Text;   
  
namespace CustomerPropertyGrid   
{   
    class XPropDescriptor : PropertyDescriptor   
    {   
        XProp theProp;   
  
        public XPropDescriptor(XProp prop, Attribute[] attrs) : base(prop.Name,attrs)   
        {   
            theProp = prop;   
        }   
  
        public override bool CanResetValue(object component)   
        {   
            return false;   
        }   
  
        public override System.Type ComponentType   
        {   
            get { return this.GetType(); }   
        }   
  
        public override object GetValue(object component)   
        {   
            return theProp.Value;   
        }   
  
        public override bool IsReadOnly   
        {   
            get { return false; }   
        }   
  
        public override System.Type PropertyType   
        {   
            get { return theProp.Value.GetType(); }   
        }   
  
        public override void ResetValue(object component)   
        {   
        }   
  
        public override void SetValue(object component, object value)   
        {   
            theProp.Value = value;   
        }   
  
        public override bool ShouldSerializeValue(object component)   
        {   
            return false;   
        }   
    }   
}  

 

這一段程式中,大部分都是返回預設值。需要注意的是下面的部分。

  • New
    這個部分把 XProp 的對象匯入到類中,儲存在變數 theProp 裡面,成為將使用者屬性和 PropertyGrid 使用的屬性對應的基礎。
  • GetValue
    把儲存在 XProp 中的屬性值放到 PropertyGrid 裡面。
  • SetValue
    把使用者通過畫面在 PropertyGrid 中修改了的屬性值儲存到 XProp 裡面。
  • PropertyType
    指定這個屬性的類型。PropertyGrid 是可以利用不同的類型來選擇對應的屬性值對話方塊的。
建立Class XProps

XProps 其實就是 XProp 的 Collection。這很容易,只要用一行代碼,繼承 System.Collections.Generic.List(Of XProp) 就搞定了。(希望你是瞭解 Generic 類的,這玩意是 .Net Framework 2.0裡面的。)

另一方面,XProps 又需要實現 ICustomTypeDescriptor 的介面,這樣把這個類的對象和 PropertyGrid 關聯起來的時候,PropertyGrid 就會使用使用者自訂的部分來構成屬性視窗,而不是用這個類本身的程式Property來構成屬性視窗。

using System.ComponentModel;   
using System.Collections.Generic;   
using System.Text;   
  
namespace CustomerPropertyGrid   
{   
    class XProps : List<XProp>, ICustomTypeDescriptor   
    {  
        #region ICustomTypeDescriptor 成員   
  
        public AttributeCollection GetAttributes()   
        {   
            return TypeDescriptor.GetAttributes(this,true);   
        }   
  
        public string GetClassName()   
        {   
            return TypeDescriptor.GetClassName(this, true);   
        }   
  
        public string GetComponentName()   
        {   
            return TypeDescriptor.GetComponentName(this, true);   
        }   
  
        public TypeConverter GetConverter()   
        {   
            return TypeDescriptor.GetConverter(this, true);   
        }   
  
        public EventDescriptor GetDefaultEvent()   
        {   
            return TypeDescriptor.GetDefaultEvent(this, true);   
        }   
  
        public PropertyDescriptor GetDefaultProperty()   
        {   
            return TypeDescriptor.GetDefaultProperty(this, true);   
        }   
  
        public object GetEditor(System.Type editorBaseType)   
        {   
            return TypeDescriptor.GetEditor(this, editorBaseType, true);   
        }   
  
        public EventDescriptorCollection GetEvents(System.Attribute[] attributes)   
        {   
            return TypeDescriptor.GetEvents(this, attributes, true);   
        }   
  
        public EventDescriptorCollection GetEvents()   
        {   
            return TypeDescriptor.GetEvents(this, true);   
        }   
  
        public PropertyDescriptorCollection GetProperties(System.Attribute[] attributes)   
        {   
            PropertyDescriptor[] props = new PropertyDescriptor[this.Count];   
            for (int i = 0; i < this.Count; i++)   
            {   
                props[i] = new XPropDescriptor(this[i], attributes);   
            }   
            return new PropertyDescriptorCollection(props);   
        }   
  
        public PropertyDescriptorCollection GetProperties()   
        {   
            return TypeDescriptor.GetProperties(this, true);   
        }   
  
        public object GetPropertyOwner(PropertyDescriptor pd)   
        {   
            return this;   
        }  
 
        #endregion   
  
        public override string ToString()   
        {   
            StringBuilder sb = new StringBuilder();   
            for (int i = 0; i < this.Count; i++)   
            {   
                sb.Append("[" + i + "] " + this[i].ToString() + System.Environment.NewLine);   
            }   
            return sb.ToString();   
        }   
    }   
}  

 

這段代碼雖然比較冗長,不過讀起來並不費力的。

ICustomTypeDescriptor 的實現部分中絕大部分是用 TypeDescriptor中 的成員實現的。只有 GetProperties 是另外寫的,這個部分也很簡單,就是把 Collection 裡面的內容構成一個 PropertyDescriptor 集合,在用這個數組構成 PropertyDescriptorCollection 返回。

需要指出的是,也可以用 Dictionary 或者其他類型的 Collection 來構成這個類,差別部分也就是GetProperties 的寫法不同而已。

剩下的部分就是對 ToString 的 Overrides 了,是為了方便後面對整個類的內容顯示。這段程式裡面使用了 StringBuilder,是屬於 System.Text 的。在進行很多字串組合的時候,這比用運算子 (&) 速度快很多。

介面代碼XProp[] xprop = new XProp[3];   
  
            xprop[0] = new XProp();   
            xprop[0].Name = "A";   
            xprop[0].Value = "1";   
            xprop[1] = new XProp();   
            xprop[1].Name = "B";   
            xprop[1].Value = "2";   
            xprop[2] = new XProp();   
            xprop[2].Name = "C";   
            xprop[2].Value = "3";   

            XProps xps=new XProps();
            xps.Add(xprop[0]);
            xps.Add(xprop[1]);
            xps.Add(xprop[2]);

            propertyGrid1.SelectedObject = xps;

url:http://greatverve.cnblogs.com/archive/2012/02/08/propertygird-bind.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.