2011-12-30 43 views
2

我想公开自定义控件中的一些属性。我需要输入三个参数,我从控件中公开为Browsable属性。根据一个物业的投入,其他两个可能不需要。如何根据第一个属性的选择来禁用/隐藏不需要的属性?禁用设计视图属性网格中的属性

回答

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

这将禁用PropertyB每当PropertyA是一个空字符串。

发现这篇文章在the Code Project描述这个过程。

+0

谢谢亲爱的会看看这个! – TheVillageIdiot 2011-12-31 14:56:25