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

可自定义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;
      }

自定义PropertyGrid控件【转】的更多相关文章

  1. C# PropertyGrid控件应用心得

    何处使用 PropertyGrid 控件 在应用程序中的很多地方,您都可以使用户与 PropertyGrid 进行交互,从而获得更丰富的编辑体验.例如,某个应用程序包含多个用户可以设置的“设置”或选项 ...

  2. C# PropertyGrid控件应用心得 【转】

    源文 : http://blog.csdn.net/luyifeiniu/article/details/5426960 c#stringattributesobjectmicrosoftclass ...

  3. PropertyGrid控件由浅入深(二):基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  4. PropertyGrid控件由浅入深(一):文章大纲

    Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...

  5. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  6. propertyGrid控件 z

    1.如果属性是enum类型,那么自然就是下拉的. 2.如果是你自定义的下拉数据,那么需要用到转换属性标签TypeConverter 参见: http://blog.csdn.net/luyifeini ...

  7. 安卓自定义组合控件--toolbar

    最近在学习安卓APP的开发,用到了toolbar这个控件, 最开始使用时include layout这种方法,不过感觉封装性不好,就又改成了自定义组合控件的方式. 使用的工具为android stud ...

  8. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  9. asp.net webform 自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: & ...

随机推荐

  1. Makefile基础(一)

    在大型的C语言项目中,一般都是由多个源文件编译链接形成的可执行程序,而这些源文件的处理步骤,通常交给Makefile来管理,Makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后 ...

  2. splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)

    先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...

  3. python--命名规范及常见的数据类型

    1.python的命名规范 (1)不能以数字开头,不能出现中文. (2)命名以字母开头,包含数字,字母(区分大小写),下划线. (3)不能包含关键字,见名知意. 2.python常见的数据类型 (1) ...

  4. PHP变量的生命周期

    变量不仅有其特定的作用范围,还有其存活的周期--生命周期.变量的生命周期指的是变量可被使用的一个时间段,在这个时间段内变量是有效的,一旦超出这个时间段变量就会失效,我们就不能够再访问到该变量的值了. ...

  5. java异常处理及400,404,500错误处理

        java代码中经常碰到各种需要处理异常的时候,比如什么IOException  SQLException  NullPointException等等,在开发web项目中,遇到异常,我现在做的就 ...

  6. C++中使用Curl和JsonCpp调用有道翻译API实现在线翻译

    使用C++开发一个在线翻译工具,这个想法在我大脑中过了好几遍了,所以就搜了下资料,得知网络上有很多翻译API,这里我选择我平时使用较多的有道翻译API进行在线翻译工具开发的练习.翻译API返回的结果常 ...

  7. 【Luogu】P2598狼和羊的故事(最小割转最大流)

    题目链接 最小割水题.入点向白点连边,白点向白点.黑点和空点连边,空点向空点和黑点连边,黑点向黑点和汇点连边.然后跑最大流即可. 话说Fd最近怎么光做水题啊……一点用都没有……qwq 我太菜了,做完一 ...

  8. 二进制<3>

    Matrix67:位运算简介及实用技巧(三) 进阶篇(2) (2010-07-27 11:10:44) 转载▼ 标签: it 分类: 老贾·OI相关 n皇后问题位运算版    n皇后问题是啥我就不说了 ...

  9. 洛谷P4175 - [CTSC2008]网络管理

    Portal Description 给出一棵\(n(n\leq8\times10^4)\)个点的带点权的树,进行\(m(m\leq8\times10^4)\)次操作,操作有两种: 修改一个点的点权. ...

  10. BZOJ4598 [Sdoi2016]模式字符串 【点分治 + hash】

    题目 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m 的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有多少对结点< ...