propertygrid控件使用

<? xml version = " 1.0 "  encoding = " gb2312 "   ?>
< Components >
  
<!--
  
< Component Name = " SelectTextBox "  Namespace = " STBox "  Asm = " E:\Visual Studio\Projects\SBZMis\SelectTextBox\SelectTextBox\bin\Debug\SelectTextBox.dll " >
    
< Propertys >
      
< Property Name = " BackColor "  Caption = " 背影色 "  Group = "" />
      
< Property Name = " Border "  Caption = " 边框样式 "  Group = "" />
      
< Property Name = " Font "  Caption = " 字体 "  Group = "" />
      
< Property Name = " ForeColor "  Caption = " 字体颜色 "  Group = "" />
      
< Property Name = " Value "  Caption = " "  Group = "" />
      
< Property Name = " MultLine "  Caption = " 多行 "  Group = "" />
      
< Property Name = " TabIndex "  Caption = " Tab顺序 "  Group = "" />
      
< Property Name = " CaptionColor "  Caption = " 标题颜色 "  Group = "" />
      
< Property Name = " EditBackColor "  Caption = " 编辑框背景色 "  Group = "" />
      
< Property Name = " LineColor "  Caption = " 下划线色 "  Group = "" />
      
< Property Name = " SameColor "  Caption = " 前后同色 "  Group = "" />
      
< Property Name = " AutoShowButton "  Caption = " 自动显示按钮 "  Group = "" />
      
< Property Name = " AutoSizeTextField "  Caption = " 自动调整文本框大小 "  Group = "" />
      
< Property Name = " ButtonCaption "  Caption = " 按钮标题 "  Group = "" />
      
< Property Name = " ButtonColor "  Caption = " 按钮颜色 "  Group = "" />
      
< Property Name = " ButtonImage "  Caption = " 按钮图片 "  Group = "" />
      
< Property Name = " CaptionFont "  Caption = " 标题字体 "  Group = "" />
      
< Property Name = " ExculdeFilter "  Caption = " 不包含过虑器 "  Group = "" />
      
< Property Name = " ExcludeFilterString "  Caption = " 不包含字串 "  Group = "" />
      
< Property Name = " IncludeFilter "  Caption = " 包含过虑器 "  Group = "" />
      
< Property Name = " IncludeFilterString "  Caption = " 包含字串 "  Group = "" />
      
< Property Name = " NoButton "  Caption = " 不显示按钮 "  Group = "" />
      
< Property Name = " TextFont "  Caption = " 编辑框字体 "  Group = "" />
      
< Property Name = " Type "  Caption = " 类型 "  Group = "" />
      
< Property Name = " ShowCaption "  Caption = " 显示标题 "  Group = "" />
      
< Property Name = " ShowScrollBar "  Caption = " 显示滚动条 "  Group = "" />
      
< Property Name = " TextAlign "  Caption = " 编辑框对齐模式 "  Group = "" />
    
</ Propertys >
    
< DataBinding >

    
</ DataBinding >
  
</ Component >
  
!-->
  
< 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 = " ListBox "  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 = " FormatString "  Caption = " 格式串 "  Group = "" />
      
< Property Name = " FormattingEnabled "  Caption = " 启用格式串 "  Group = "" />
      
< Property Name = " Items "  Caption = " 列表项目 "  Group = "" />
      
< Property Name = " MultColumn "  Caption = " 多列 "  Group = "" />
      
< Property Name = " IntegralHeight "  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 = "" />
        
< Property Name = " ImageList "  Caption = " 图象 "  Group = "" />
        
< Property Name = " ImageAlign "  Caption = " 图象对其的方式 "  Group = "" />
        
< Property Name = " Image "  Caption = " 控件显示的图象 "  Group = "" />
    
</ Propertys >
    
< DataBinding >

    
</ DataBinding >

  
</ Component >
</ Components >
using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.Text;
using  System.ComponentModel;
using  System.Xml;
using  System.Windows.Forms;
namespace  WindowsApplication1
{
  
class CustomProperty : ICustomTypeDescriptor
  
{
    
//当前选择对象
    private object mCurrentSelectObject;
    
private Dictionary<stringstring> mObjectAttribs = new Dictionary<stringstring>();
    
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);
      }

    }

      
    
ICustomTypeDescriptor Members
    
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);
      }

    }

  }

}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
    XmlDocument mXDoc = new XmlDocument();
    public Form1()
    {
      InitializeComponent();
      mXDoc.Load(Application.StartupPath + "\\Components.xml");
    }

    private void button1_Click(object sender, EventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"Button\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"ListBox\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"TextBox\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;

    }

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

    }
  }
}

你可能感兴趣的:(property)