各位,我现在有一个Button,Button绑定到PropertyGrid上
例如:
    Button btn = new Button();
    PropertyGrid.SelectObject = btn;这样PropertyGrid便会显示这个btn的所有Property,现在我不想让ProperytGrid显示所有的Property,只让PropertyGrid显示只定的Property,例如,我只让PropertyGrid显示btn的Text,Name两个属性。
这怎样做到?

解决方案 »

  1.   

    我也想知道.
    我只知道设计时使用Designer来实现的!
      

  2.   

    我现在的方案时,利用codeDom生成一个类,这个类包括指定的所有属性(例如:Text,Name),然后将这个类绑定到PropertyGrid上,然后当改变PropertyGrid上Property的值时,通过这个类再设置btn的属性。
      

  3.   

    有没有更好的办法,利用通过某咱方法过滤掉不显示的属性。
    或者通过反射在btn的Property上添加[BrowerAble(false)],是否可以实现?
      

  4.   

    BrowerAble(false) 可以实现,不过有属性特性    class Test
        {
            public string Property1
            {
                get
                {
                    return string.Empty;
                }
            }        [UserBrowerAble()]
            public string Property2
            {
                get
                {
                    return Guid.Empty.ToString();
                }
            }
        }    class UserBrowerAbleAttribute : System.Attribute
        {
        }            AttributeCollection ac = new AttributeCollection(new UserBrowerAbleAttribute());
                this.propertyGrid1.BrowsableAttributes = ac;Broweable等属性仍有效
      

  5.   

    能不能说的详细一点,如何只让button在PropertyGrid里只显示text,name这两个属性
      

  6.   

    能不能说的详细一点,如何只让button在PropertyGrid里只显示text,name这两个属性,通过什么方法可以过滤掉不让显示的属性?
      

  7.   

    在button的text 和 name 上加下 [UserBrowerAble()]
      

  8.   

    Button btn = new Button();
        PropertyGrid.SelectObject = btn;
    这样自样加?怎样把[UserBrowAble()]添加到text和name上?
      

  9.   

    http://www.codeproject.com/csharp/dzdynamicproperties.asp
    我找到了!
    if(company.PropertyCommands.Contains("Address"))
    {
    company.PropertyCommands["Address"].ReadOnly=!company.PropertyCommands["Address"].ReadOnly;
    }
    else
    {
    company.PropertyCommands.Add(new CustomControls.HelperClasses.PropertyCommand("Address", true, true));
    }
    pg.Refresh();
      

  10.   

    http://www.blabla.cn/vb2005/s_propgrid03.html
      

  11.   

    [Designer(typeof(ButtonFilterPropertiesDesigner))]
    public class MyButton : System.Windows.Forms.Button{}class ButtonFilterPropertiesDesigner : System.Windows.Forms.Design.ControlDesigner{
    //去掉不想显示的属性
    protected override void PreFilterProperties(System.Collections.IDictionary properties) {
    properties.Remove("AccessibilityObject");
    properties.Remove("AccessibleDefaultActionDescription");
    properties.Remove("AccessibleDescription");
    //去掉不想显示的事件
    protected override void PreFilterEvents(System.Collections.IDictionary events) {
    events.Remove("AutoSizeChanged");
    events.Remove("BackColorChanged");
    events.Remove("BackgroundImageChanged");
    }
    }