using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Demo
{
public partial class PropertyGrid : Form
{
public PropertyGrid()
{
InitializeComponent();
}
class Property
{
private string _width = "50";
public string Width
{
get { return _width; }
set { _width = Width; }
}
private string _height = "50";
public string Height
{
get { return _height; }
set { _height = Height; }
}
private float _TieMu = 5.5f;
private string _Rule = "数子法";
[CategoryAttribute("规则"), DescriptionAttribute("贴目")]
public float TieMu
{
get { return _TieMu; }
set { _TieMu = TieMu; }
}
[CategoryAttribute("规则"), DescriptionAttribute("计算法")]
public string Rule
{
get { return _Rule; }
set { _Rule = Rule; }
}
private int _Black = 0;
private int _White = 0;
[CategoryAttribute("围棋"), DescriptionAttribute("黑")]
public int Black
{
get { return _Black; }
set { _Black = Black; }
}
[CategoryAttribute("围棋"), DescriptionAttribute("白"),DisplayName("围棋属性")]
public int White
{
get { return _White; }
set { _White = White; }
}
}
/// <summary>
/// 设置控件可见
/// </summary>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <param name="visible"></param>
public void SetPropertyVisibility(object obj, string propertyName, bool visible)
{
Type type = typeof(BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
fld.SetValue(attrs[type], visible);
}
private void PropertyGrid_Load(object sender, EventArgs e)
{
propertyGrid_T.SelectedObject = new Property();
}
private void Btn_Modify_Click(object sender, EventArgs e)
{
SetPropertyVisibility(propertyGrid_T, "围棋", false);
}
}
}