2011-12-30 8 views
2

カスタムコントロールの一部のプロパティを公開したいとします。私はコントロールからのプロパティとしてBrowsableとして公開する3つのパラメータの入力を取得する必要があります。 1つのプロパティの入力に基づいて、他の2つは必要ないかもしれません。どのように私は最初のプロパティの選択に基づいて必要ではないプロパティを無効/非表示にすることができますか?デザインビューのプロパティを無効にするプロパティグリッド

答えて

3

はい、少し反射して、あなたはこの達成することができます:PropertyAが空の文字列である時はいつでも

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; } 
    } 
} 

をこれはPropertyBが無効になります。

この記事はthe Code Projectで、このプロセスが記述されています。

+0

ありがとう、おかげでこれが検討されます! – TheVillageIdiot

関連する問題