Mic*_*kus 5 .net c# propertygrid readonly winforms
我有一个PropertyGrid用于在助手类中显示属性的.我将helper类指定为PropertyGrid:
myPropertyGrid.SelectedObject = mySettingsHelper;
Run Code Online (Sandbox Code Playgroud)
在帮助器类中,我ReadOnlyAttribute在设计时分配如下:
[DisplayName("DisplayExA"),
Description("DescriptionExA"),
ReadOnlyAttribute(true)]
public string PropertyA { get; set; }
[DisplayName("DisplayExB"),
Description("DescriptionExB"),
ReadOnlyAttribute(false)]
public string PropertyB { get; set; }
[DisplayName("DisplayExC"),
Description("DescriptionExC"),
ReadOnlyAttribute(true)]
public string PropertyC { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是现在我需要能够在运行时动态地更改各个属性的这个属性.根据某些标准,这些属性中的一些可能需要是只读的或不是只读的.如何在运行时动态进行更改?
编辑:
我尝试了以下代码,但这为对象的每个实例设置了ReadOnly属性!我想按对象做.有时一个对象可能具有PropertyA只读,而第二个对象的PropertyA不是只读的.
public static class PropertyReadOnlyHelper
{
public static void SetReadOnly(object container, string name, bool value)
{
try
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container.GetType())[name];
ReadOnlyAttribute attribute = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo fieldToChange = attribute.GetType().GetField("isReadOnly",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
fieldToChange.SetValue(attribute, value);
}
catch { }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11187 次 |
| 最近记录: |