zoukankan      html  css  js  c++  java
  • C#中的PropertyGrid绑定对象,通过改变某一值而动态设置部分属性的特性

    问题:如下,我定义了一个对象,默认设置属性WindowSize ,WindowSize 为不可见,通过改变SaveOnClose的值,动态的改变不可见的属性的显示和隐藏。

     [DefaultPropertyAttribute("SaveOnClose")]  
         public class AppSettings{  
              private bool saveOnClose = false;  
              private Size windowSize = new Size(100,100);  
              private Font windowFont = new Font("宋体", 9, FontStyle.Regular);  
              [CategoryAttribute("文档设置"),  
              DefaultValueAttribute(true)]  
              public bool SaveOnClose  
              {  
                  get { return saveOnClose; }  
                  set { saveOnClose = value;}  
              }  
              [CategoryAttribute("文档设置"),
               Browsable(false)]  
              public Size WindowSize   
              {  
                  get { return windowSize; }  
                  set { windowSize = value;}  
              }  
              [CategoryAttribute("文档设置"),
               Browsable(false),]  
              public Font WindowFont   
              {  
                  get {return windowFont; }  
                  set { windowFont = value;}  
              }  
    }

    那么,现在,既然有属性的特性Browsable,可以设置属性的显示和隐藏,我们就可以通过改变这个参数的值,从而达到我们想要的效果。如下:

           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);
            }
            void SetPropertyReadOnly(object obj, string propertyName, bool readOnly)
            {
                Type type = typeof(System.ComponentModel.ReadOnlyAttribute);
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
                AttributeCollection attrs = props[propertyName].Attributes;
                FieldInfo fld = type.GetField("isReadOnly", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
                fld.SetValue(attrs[type], readOnly);
            }

    以上方法,不用我说。一看就知道用途了吧,通过如下方法去调用:

    private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
            {
                var item = propertyGrid1.SelectedObject;
                bool b = (bool)e.ChangedItem.Value;
                if (b)
                {
                    SetPropertyVisibility(item, "WindowSize", b);//WindowSize必须是自定义属性中的属性名,如下也是
                    SetPropertyVisibility(item, "WindowFont", b);
                    //SetPropertyReadOnly(item, "WindowFont", b);
                }
                else
                {
                    SetPropertyVisibility(item, "WindowSize", b);
                    SetPropertyVisibility(item, "WindowFont", b);
                    //SetPropertyReadOnly(item, "WindowFont", b);
                }
                propertyGrid1.SelectedObject = item;  
            }

    通过以上方法就可以达到效果了。 需要注意几点:

      1.SetPropertyVisibility方法,SetPropertyReadOnly方法中的参数分别指:(item,’自定义属性对象中的某个属性名‘,值)

       2.上述方法调用完后,记得需要再次 propertyGrid1.SelectedObject = item; 否则,是没有效果的

    参考文献:http://bbs.csdn.net/topics/350150747

  • 相关阅读:
    Android窗口管理服务WindowManagerService计算Activity窗口大小的过程分析
    软件的三层架构
    【Android小应用】颈椎保健操Android开源项目
    huffman编码——原理与实现
    30天自制操作系统之第13天 定时器(2)
    JSP中Session的使用
    [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
    在C语言中,double、long、unsigned、int、char类型数据所占字节数
    nyoj 130 同样的雪花 【哈希】
    socketpair的使用
  • 原文地址:https://www.cnblogs.com/ysq0908/p/6736099.html
Copyright © 2011-2022 走看看