【问题标题】:Change .NET Property Grid readonly attribute at runtime在运行时更改 .NET 属性网格只读属性
【发布时间】:2014-02-27 05:50:08
【问题描述】:

我有一个属性网格,我想在运行时更改它的一些项目的只读属性。 为简单项目更改只读很容易,我在那里没有问题,我的问题是我无法为下图中的纬度和经度等分层项目更改只读。我什至尝试将整个“职位”类别设为只读,但似乎没有帮助。

附带问题:有没有办法将一些项目放在一个组中(比如这个位置项目)而不将它们包装在一个类中?

【问题讨论】:

  • 祝你好运。我和一位同事一起工作,他必须编写各种子类才能获得我认为你描述的行为

标签: c# winforms propertygrid


【解决方案1】:

是的!这可能会有所帮助!

public class Member
{
string name;
bool isMarried;
string spouseName;

public string Name
{
get { return name; }
set { name = value; }
}

[System.ComponentModel.RefreshProperties(RefreshProperties.All)]
public bool IsMarried
{
get { return isMarried; }
set
{
isMarried = value;
bool newValue = !value;
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["SpouseName"];
ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attrib, newValue);
}
}

[ReadOnly(true)]
public string SpouseName
{
get { return spouseName; }
set
{
spouseName = value;
}
}
}

【讨论】:

  • 当我以只读方式运行此代码时,所有“成员”实例的“SpousName”都发生了变化!
  • 注意:刷新 PropertyGrid 以显示更改。
猜你喜欢
  • 1970-01-01
  • 2010-12-05
  • 2011-01-23
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多