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

posted @ 2017-04-19 21:51  goodTOgreat  阅读(7198)  评论(0编辑  收藏  举报