最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)ProperTyGrid自定義屬性的方法

 更新時間:2014年09月06日 15:07:15   投稿:shichen2014  
這篇文章主要介紹了C#實現(xiàn)ProperTyGrid自定義屬性的方法,主要通過接口ICustomTypeDescriptor實現(xiàn),需要的朋友可以參考下

本文實例講解了C#實現(xiàn)ProperTyGrid自定義屬性的方法,分享給大家供大家參考。具體方法如下:

一般來說,C#如果要實現(xiàn)自定義屬性必須要需要實現(xiàn)接口ICustomTypeDescriptor,具體實現(xiàn)方法如下:

// 摘要: 
// 提供為對象提供動態(tài)自定義類型信息的接口。 
public interface ICustomTypeDescriptor

示例如下:

/// <summary>
/// 自定義屬性對象
/// </summary>
public class MyAttr
{
    private string name = string.Empty;

    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    private object value = null;

    public object Value
    {
      get { return this.value; }
      set { this.value = value; }
    }

    private string description = string.Empty;

    public string Description
    {
      get { return description; }
      set { description = value; }
    }

    public override string ToString()
    {
      return string.Format("Name:{0},Value:{1}",name.ToString(),value.ToString());
    }
}

/// <summary>
/// 自定義性質(zhì)描述類
/// </summary>
public class MyPropertyDescription : PropertyDescriptor
{
    private MyAttr myattr = null;
    public MyPropertyDescription(MyAttr myattr, Attribute[] attrs): base(myattr.Name, attrs) 
    {
      this.myattr = myattr;
    }
    public override bool CanResetValue(object component)
    {
      return false;
    }

    public override Type ComponentType
    {
      get
      {
        return this.GetType();
      }
    }

    public override object GetValue(object component)
    {
      return myattr.Value;
    }

    public override bool IsReadOnly
    {
      get 
      {
        return false;
      }
    }

    public override Type PropertyType
    {
      get 
      {
        return myattr.Value.GetType();
      }
    }

    public override void ResetValue(object component)
    {
      //不重置,無動作 
    }

    public override void SetValue(object component, object value)
    {
      myattr.Value = value;
    }
    /// <summary>
    /// 是否應(yīng)該持久化保存
    /// </summary>
    /// <param name="component"></param>
    /// <returns></returns>
    public override bool ShouldSerializeValue(object component)
    {
      return false;
    }
    /// <summary>
    /// 屬性說明
    /// </summary>
    public override string Description
    {
      get
      {
        return myattr.Description;
      }
    }
}

/// <summary>
/// 實現(xiàn)自定義的特殊屬性對象必須繼承ICustomTypeDescriptor,并實現(xiàn)Dictionary
/// </summary>
public class MyAttrCollection : Dictionary<String, MyAttr>, ICustomTypeDescriptor
{
    /// <summary>
    /// 重寫Add方法
    /// </summary>
    /// <param name="attr"></param>
    public void Add(MyAttr attr) 
    {
      if (!this.ContainsKey(attr.Name))
      {
        base.Add(attr.Name, attr);
      }
    }

    public AttributeCollection GetAttributes()
    {
      return TypeDescriptor.GetAttributes(this, true);
    }

    public string GetClassName()
    {
      return TypeDescriptor.GetClassName(this,true);
    }

    public string GetComponentName()
    {
      return TypeDescriptor.GetClassName(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(Type editorBaseType)
    {
      return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
      return TypeDescriptor.GetEvents(this, attributes, true);
    }

    public EventDescriptorCollection GetEvents()
    {
      return TypeDescriptor.GetEvents(this, true);
    }

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
      int count=this.Values.Count;
      PropertyDescriptor[] pds=new PropertyDescriptor[count];
      int index = 0;
      foreach (MyAttr item in this.Values)
      {
        pds[index] = new MyPropertyDescription(item,attributes);
        index++;
      }
      return new PropertyDescriptorCollection(pds);
    }

    public PropertyDescriptorCollection GetProperties()
    {
      return TypeDescriptor.GetProperties(this,true);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
      return this;
    }
}

前臺調(diào)用如下圖所示:

private void btnAddProperType_Click(object sender, EventArgs e)
{
  MyAttr attr = new MyAttr();
  attr.Name = txtName.Text.Trim();
  attr.Value = txtValue.Text.Trim();
  attr.Description = txtDescription.Text.Trim();
  mac.Add(attr);
  MyGrid.Refresh();
}

private void button1_Click(object sender, EventArgs e)
{
  AddAttrColor();
  AddAttrImage();
  AddAttrEmun();
  MyGrid.Refresh();
}

private void AddAttrEmun()
{
  MyAttr attr = new MyAttr();
  attr.Name = "Dock";
  attr.Value = DockStyle.Fill;
  attr.Description = "枚舉";
  mac.Add(attr);
}

private void AddAttrImage()
{
  MyAttr attr = new MyAttr();
  attr.Name = "Image";
  attr.Value = new Bitmap(400,300);
  attr.Description = "圖片";
  mac.Add(attr);
}

private void AddAttrColor()
{
  MyAttr attr = new MyAttr();
  attr.Name = "Color";
  attr.Value = Color.Red;
  attr.Description = "顏色";
  mac.Add(attr);
}

運行效果如下圖所示:

希望本文所述對大家的C#程序設(shè)計有所幫助

相關(guān)文章

  • 在C#里面給PPT文檔添加注釋的實現(xiàn)代碼

    在C#里面給PPT文檔添加注釋的實現(xiàn)代碼

    平常開會或者做總結(jié)報告的時候我們通常都會用到PowerPoint演示文稿,我們可以在單個幻燈片或者全部幻燈片里面添加注釋,這樣觀眾可以從注釋內(nèi)容里面獲取更多的相關(guān)信息,需要的朋友可以參考下
    2017-01-01
  • c# 如何用組合替代繼承

    c# 如何用組合替代繼承

    這篇文章主要介紹了c# 如何用組合替代繼承,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-02-02
  • C#判斷字符是否為漢字的三種方法分享

    C#判斷字符是否為漢字的三種方法分享

    判斷一個字符是不是漢字通常有三種方法,第一種用 ASCII 碼判斷,第二種用漢字的 UNICODE 編碼范圍判 斷,第三種用正則表達式判斷,以下是具體方法
    2014-01-01
  • C#實現(xiàn)彈窗提示輸入密碼

    C#實現(xiàn)彈窗提示輸入密碼

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)彈窗提示輸入密碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#使用webbrowser的常見用法實例

    C#使用webbrowser的常見用法實例

    這篇文章主要介紹了C#使用webbrowser的常見用法,涉及C#使用webbrowser實現(xiàn)判斷網(wǎng)絡(luò)連接、模擬登陸、點擊等常用技巧,需要的朋友可以參考下
    2015-08-08
  • C#判斷數(shù)據(jù)類型的簡單示例代碼

    C#判斷數(shù)據(jù)類型的簡單示例代碼

    本篇文章要是對C#中判斷數(shù)據(jù)類型的簡單示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#實現(xiàn)Winform鼠標(biāo)拖動窗口大小時設(shè)定窗口最小尺寸的方法

    C#實現(xiàn)Winform鼠標(biāo)拖動窗口大小時設(shè)定窗口最小尺寸的方法

    這篇文章主要介紹了C#實現(xiàn)Winform鼠標(biāo)拖動窗口大小時設(shè)定窗口最小尺寸的方法,涉及WinForm改變窗口大小時動態(tài)判斷當(dāng)前窗口尺寸的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
    2015-11-11
  • C#操作注冊表的方法

    C#操作注冊表的方法

    這篇文章介紹了C#操作注冊表的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • c#設(shè)計模式 適配器模式詳細(xì)介紹

    c#設(shè)計模式 適配器模式詳細(xì)介紹

    結(jié)構(gòu)模式(Structural Pattern)描述如何將類或者對象結(jié)合在一起形成更大的結(jié)構(gòu)。結(jié)構(gòu)模式描述兩種不同的東西:類與類的實例。根據(jù)這一點,結(jié)構(gòu)模式可以分為類的結(jié)構(gòu)模式和對象的結(jié)構(gòu)模式
    2012-10-10
  • 淺談Visual Studio 2019 Vue項目的目錄結(jié)構(gòu)

    淺談Visual Studio 2019 Vue項目的目錄結(jié)構(gòu)

    這篇文章主要介紹了Visual Studio 2019 Vue項目 目錄結(jié)構(gòu),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論

宜春市| 孟村| 枣庄市| 泽普县| 鞍山市| 镇平县| 田东县| 普格县| 定边县| 香格里拉县| 乌拉特后旗| 六枝特区| 木兰县| 华安县| 常山县| 淄博市| 肇东市| 大丰市| 五大连池市| 依兰县| 平罗县| 彝良县| 莲花县| 乌拉特中旗| 淳安县| 清徐县| 清新县| 泰州市| 澳门| 民丰县| 柞水县| 德清县| 瑞丽市| 南京市| 佛坪县| 梅州市| 玉树县| 邻水| 临漳县| 资溪县| 镇原县|