C#下PropertyGrid使用

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值