我想公开自定义控件中的一些属性。我需要从控件中获取三个参数的输入,这些参数公开为 Browsable
属性。根据一个属性的输入,可能不需要其他两个属性。如何根据第一个属性的选择禁用/隐藏不需要的属性?
最佳答案
是的,只要稍加思考,您就可以实现这一目标:
public class TestControl : Control {
private string _PropertyA = string.Empty;
private string _PropertyB = string.Empty;
[RefreshProperties(RefreshProperties.All)]
public string PropertyA {
get { return _PropertyA; }
set {
_PropertyA = value;
PropertyDescriptor pd = TypeDescriptor.GetProperties(this.GetType())["PropertyB"];
ReadOnlyAttribute ra = (ReadOnlyAttribute)pd.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo fi = ra.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(ra, _PropertyA == string.Empty);
}
}
[RefreshProperties(RefreshProperties.All)]
[ReadOnly(true)]
public string PropertyB {
get { return _PropertyB; }
set { _PropertyB = value; }
}
}
每当 PropertyA 为空字符串时,这将禁用 PropertyB。
在the Code Project找到这篇文章描述了这个过程。
关于.net - 在设计 View 属性网格中禁用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676996/