propertyGrid动态(runtime)改变item的readonly 和browsable

来源:互联网 发布:中小型软件构架实践 编辑:程序博客网 时间:2024/06/06 01:59

        public void SetDatatypeShowOrHide(bool isShow)        {            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["IRButtonZWaveNodeId"];            BrowsableAttribute attri =(BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];            FieldInfo isBrow = attri.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);            isBrow.SetValue(attri, isShow);        }

"DataType"为propertyGrid中显示的属性。通过调用SetDatatypeShowOrHide()函数就可以动态设置DataType属性的显示和隐藏。

public void SetDatatypeReadOnly(bool isReadOnly) {

PropertyDescriptor descriptor =     TypeDescriptor.GetProperties(this.GetType())["DataType"];ReadOnlyAttribute attrib =     (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];FieldInfo isReadOnly =  attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic| BindingFlags.Instance); isReadOnly.SetValue(attrib, isReadOnly);

}

通过调用函数就可以在程序的运行期间动态的设置属性是否Readonly。

原创粉丝点击