隐藏组合控件的某个子控件属性问题

来源:互联网 发布:华美淘宝客使用说明 编辑:程序博客网 时间:2024/06/06 02:11

     写了个想隐藏一个组合控件的子控件属性的 Designer,只要加在控件上,窗体以及该窗体上的控件的 所有属性都被隐藏了,

     我只想显示DemoControl上面子控件 Box的 标记了CustomHelperAttribute 特性的 属性,其余都隐藏, 结果所有的都隐藏了。

     大家帮忙分析分析下。

    [Designer(typeof(ChildrenPropertiesFilterDesigner), typeof(IDesigner))]
    public class DemoControl: Control

    {  

                private TextBox box;

                public TextBox Box

{     

                     get{   return box   ;} 

               }

                public  DemoControl()

{

box = new TextBox();

                //此处一些代码省略。。。

                this.Controls.Add(box);

}

                 [CustomHelperAttribute ("仅显示这个属性。", true)]

                public string ShowMe

                {

                          get; set;

                }

    }


   public class ChildrenPropertiesFilterDesigner : ControlDesigner

    {
        /// <summary>
        /// 设置控件的属性Visible
        /// </summary>
        /// <param name="component"></param>
        private void SetPropertyVisible(Object component)
        {
            Type browseType = typeof(BrowsableAttribute);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(component);
            foreach (PropertyDescriptor p in properties)
            {
                AttributeCollection attrs = properties[p.Name].Attributes;
                AttributeCollection piAtrrs = p.Attributes;
                bool visible = false;
                foreach (Attribute a in piAtrrs)
                {
                    if (a.GetType() == typeof(CustomHelperAttribute))
                    {
                        CustomHelperAttribute ca = (CustomHelperAttribute)a;
                        visible = ca.AllowShow;
                        break;
                    }
                }


                if (visible == false)
                {
                    FieldInfo field = browseType.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
                    field.SetValue(attrs[browseType], visible);
                }
            }
        }


        protected override void PreFilterProperties(IDictionary properties)
        {
            TextBox box = (TextBox)base.Control.Controls[0];
            if (box != null)
            {
                SetPropertyVisible(box);
            }


            base.PreFilterProperties(properties);
        }
    }