自定义PropertyGrid控件

     这篇随笔其实是从别人博客上载录的。感觉很有价值,整理了一下放在了我自己的博客上,希望原作者不要介意。

     可自定义PropertyGrid控件的属性。也可将属性名称显示为中文。主要是由XML文件与ICustomTypeDescriptor来实现。

第一步:做一个继承ICustomTypeDescriptor接口的类 文件名称:CustomProperty.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Windows.Forms;

namespace  **
{
  class CustomProperty : ICustomTypeDescriptor
  {
    //当前选择对象
    private object mCurrentSelectObject;
    private Dictionary<string, string> mObjectAttribs = new Dictionary<string, string>();
    public CustomProperty(object pSelectObject, XmlNodeList pObjectPropertys)
    {
      mCurrentSelectObject = pSelectObject;
      XmlNode tmpXNode;
      IEnumerator tmpIe = pObjectPropertys.GetEnumerator();
      while (tmpIe.MoveNext())
      {
        tmpXNode = tmpIe.Current as XmlNode;
        mObjectAttribs.Add(tmpXNode.Attributes["Name"].Value, tmpXNode.Attributes["Caption"].Value);
      }
    }
    #region ICustomTypeDescriptor Members
    public AttributeCollection GetAttributes()
    {
      return TypeDescriptor.GetAttributes(mCurrentSelectObject);
    }
    public string GetClassName()
    {
      return TypeDescriptor.GetClassName(mCurrentSelectObject);
    }
    public string GetComponentName()
    {
      return TypeDescriptor.GetComponentName(mCurrentSelectObject);
    }
    public TypeConverter GetConverter()
    {
      return TypeDescriptor.GetConverter(mCurrentSelectObject);
    }
    public EventDescriptor GetDefaultEvent()
    {
      return TypeDescriptor.GetDefaultEvent(mCurrentSelectObject);
    }
    public PropertyDescriptor GetDefaultProperty()
    {
      return TypeDescriptor.GetDefaultProperty(mCurrentSelectObject);
    }
    public object GetEditor(Type editorBaseType)
    {
      return TypeDescriptor.GetEditor(mCurrentSelectObject, editorBaseType);
    }
    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
      return TypeDescriptor.GetEvents(mCurrentSelectObject, attributes);
    }
    public EventDescriptorCollection GetEvents()
    {
      return TypeDescriptor.GetEvents(mCurrentSelectObject);
    }
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
      List<CustomPropertyDescriptor> tmpPDCLst = new List<CustomPropertyDescriptor>();
      PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(mCurrentSelectObject, attributes);
      IEnumerator tmpIe = tmpPDC.GetEnumerator();
      CustomPropertyDescriptor tmpCPD;
      PropertyDescriptor tmpPD;
      while (tmpIe.MoveNext())
      {
        tmpPD = tmpIe.Current as PropertyDescriptor;
        if (mObjectAttribs.ContainsKey(tmpPD.Name))
        {
          tmpCPD = new CustomPropertyDescriptor(mCurrentSelectObject, tmpPD);
          tmpCPD.SetDisplayName(mObjectAttribs[tmpPD.Name]);
          tmpCPD.SetCategory(tmpPD.Category + "中文"); 
          tmpPDCLst.Add(tmpCPD);
        }
      }
      return new PropertyDescriptorCollection(tmpPDCLst.ToArray());
    }
    public PropertyDescriptorCollection GetProperties()
    {
      return TypeDescriptor.GetProperties(mCurrentSelectObject);
    }
   public object GetPropertyOwner(PropertyDescriptor pd)
    {
      return mCurrentSelectObject;
    }
    #endregion
    class CustomPropertyDescriptor : PropertyDescriptor
    {
      private PropertyDescriptor mProp;
      private object mComponent;

      public CustomPropertyDescriptor(object pComponent, PropertyDescriptor pPD)
        : base(pPD)
      {
        mCategory = base.Category;
        mDisplayName = base.DisplayName;
        mProp = pPD;
        mComponent = pComponent;
      }
      private string mCategory;
      public override string Category
      {
        get { return mCategory; }
      }
      private string mDisplayName ;
      public override string DisplayName
      {
        get { return mDisplayName; }
      }
      public void SetDisplayName(string pDispalyName)
      {
        mDisplayName = pDispalyName;
      }
      public void SetCategory(string pCategory)
      {
        mCategory = pCategory;
      }
      public override bool CanResetValue(object component)
      {
        return mProp.CanResetValue(component);
      }

      public override Type ComponentType
      {
        get { return mProp.ComponentType; }
      }

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

      public override bool IsReadOnly
      {
        get { return mProp.IsReadOnly; }
      }

      public override Type PropertyType
      {
        get { return mProp.PropertyType; }
      }
      public override void ResetValue(object component) { mProp.ResetValue(component); }
      public override void SetValue(object component, object value) { mProp.SetValue(component, value); }
      public override bool ShouldSerializeValue(object component)
      {
        return mProp.ShouldSerializeValue(component);
      }
    }

    #region ICustomTypeDescriptor 成员

    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    string ICustomTypeDescriptor.GetClassName()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    string ICustomTypeDescriptor.GetComponentName()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    TypeConverter ICustomTypeDescriptor.GetConverter()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    #endregion
}
}

第二步:XML文件的处理 做一个XML文件名为CustomProperty.xml

<?xml version="1.0" encoding="gb2312" ?>
<Components>

  <Component Name="TextBox" Namespace="System.Windows.Forms" Asm="System.dll">
    <Propertys>
      <Property Name="BackColor" Caption="背影色" Group=""/>
      <Property Name="BorderStyle" Caption="边框样式" Group=""/>
      <Property Name="Font" Caption="字体" Group=""/>
      <Property Name="ForeColor" Caption="字色" Group=""/>
      <Property Name="Text" Caption="内容" Group=""/>
      <Property Name="ScrollBars" Caption="滚动条" Group=""/>
      <Property Name="TextAlign" Caption="文本对齐" Group=""/>
      <Property Name="Multline" Caption="多行" Group=""/>
      <Property Name="PasswordChar" Caption="密码文本" Group=""/>
      <Property Name="Size" Caption="大小" Group=""/>
      <Property Name="Location" Caption="位置" Group=""/>
    </Propertys>
    <DataBinding>

    </DataBinding>
  </Component>

  <Component Name="Label" Namespace="System.Windows.Forms" Asm="System.dll">
    <Propertys>
      <Property Name="BackColor" Caption="背影色" Group=""/>
      <Property Name="BorderStyle" Caption="边框样式" Group=""/>
      <Property Name="Font" Caption="字体" Group=""/>
      <Property Name="ForeColor" Caption="字色" Group=""/>
      <Property Name="Image" Caption="图片" Group=""/>
      <Property Name="ImageAlign" Caption="图片对齐" Group=""/>
      <Property Name="Text" Caption="文字" Group=""/>
      <Property Name="TextAlign" Caption="文本对齐" Group=""/>
      <Property Name="Size" Caption="大小" Group=""/>
      <Property Name="Location" Caption="位置" Group=""/>
    </Propertys>
    <DataBinding>

    </DataBinding>
  </Component>

  <Component Name="Button" Namespace="System.Windows.Forms" Asm="System.dll">
    <Propertys>
      <Property Name="BackColor" Caption="背影色" Group=""/>
      <Property Name="BorderStyle" Caption="边框样式" Group=""/>
      <Property Name="Font" Caption="字体" Group=""/>
      <Property Name="ForeColor" Caption="字色" Group=""/>
      <Property Name="FlatStyle" Caption="样式" Group=""/>
      <Property Name="Text" Caption="文本" Group=""/>
      <Property Name="Size" Caption="大小" Group=""/>
      <Property Name="Location" Caption="位置" Group=""/>
    </Propertys>
    <DataBinding>

    </DataBinding>

  </Component>
</Components>

 

第三步:winfrom中,单击"Button"、"Label"、"TextBox" 均可显示各自不同的属性页,且为中文

    XmlDocument mXDoc = new XmlDocument();
    public Form1()
    {
       mXDoc.Load(Application.StartupPath + "\\Components.xml");
    }

    private void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
        ShowCustProperty(sender, "TextBox");
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        ShowCustProperty(sender, "Label");
    }
      private void ShowCustProperty(object sender, string str)
      {
          XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name='" + str + "']");
          XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
          CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
          propertyGrid1.SelectedObject = cp;
      }

你可能感兴趣的:(property)