アプリケーションでPropertyGridを使用しています。カスタムデータ基準の実行時に、一部のプロパティの可視性と読み取り専用を変更する必要がありました。
簡単で準備ができているものは見つかりませんでしたが、実行時ReadOnlyAttribute
にBrowsableAttribute
プロパティを次のように変更することで回避策を見つけました。
protected void SetBrowsable(string propertyName, bool value)
{
PropertyDescriptor property = TypeDescriptor.GetProperties(GetType())[propertyName];
BrowsableAttribute att = (BrowsableAttribute)property.Attributes[typeof(BrowsableAttribute)];
FieldInfo cat = att.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
if (property.Attributes.Cast<Attribute>().Any(p => p.GetType() == typeof(BrowsableAttribute)))
cat.SetValue(att, value);
}
protected void SetReadOnly(string propertyName, bool value)
{
PropertyDescriptor property = TypeDescriptor.GetProperties(GetType())[propertyName];
ReadOnlyAttribute att = (ReadOnlyAttribute)property.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo cat = att.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
if (property.Attributes.Cast<Attribute>().Any(p => p.GetType() == typeof(ReadOnlyAttribute)))
cat.SetValue(att, value);
}
さて、私の問題は、これらのメソッドをどこで呼び出すべきかということです。object
これらのメソッドを呼び出すために処理できるイベントはありますか?たぶん、インターフェースを実装することによって。