2

カスタム コントロールでいくつかのプロパティを公開したいと考えています。Browsableコントロールからプロパティとして公開する 3 つのパラメーターの入力を取得する必要があります。1 つのプロパティへの入力に基づいて、他の 2 つは必要ない場合があります。最初のプロパティの選択に基づいて不要なプロパティを無効/非表示にするにはどうすればよいですか?

4

1 に答える 1

3

はい、少し考えれば、これを達成できます。

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が無効になります。

このプロセスを説明しているCodeProjectでこの記事を見つけました。

于 2011-12-30T15:29:15.867 に答える