Remove GenerateMember and Modifiers Properties in Designer

本秂侑毒 提交于 2020-01-10 05:15:17

问题


I created a Button descendant where I hide all the properties I don't use.

I do it like this:

[Browsable(false)]
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("", true)]
public new Boolean AllowDrop { get; set; }

Most properties get correctly hidden and cannot be used.

However there are two properties that I cannot get rid of.

Is there a way to also remove GenerateMember and Modifiers in the Designer?


回答1:


You can create a new ControlDesigner for your control and override its PostFilterProperties method. The method lets you to change or remove the items within the dictionary of properties.

The keys in the dictionary of properties are the names of the properties. Although Modifiers and GenerateMember are not actual properties of your control and they are design-time properties, but you can remove them this way:

using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(MyCustomControlDesigner))]
public class MyCustomControl:Button
{
}
public class MyCustomControlDesigner:ControlDesigner
{
   protected override void PostFilterProperties(System.Collections.IDictionary properties)
   {
       base.PostFilterProperties(properties);
       properties.Remove("Modifiers");
       properties.Remove("GenerateMember");
   }
}

To hide properties in property grid, Instead of overriding or shadowing them, you can do the same thing for them.




回答2:


I don't think you can remove it because it is not a Class Property but a Design-Time Property and used only by the designer:

If you have played with Whidbey builds of Visual Studio, you may have noticed this new property called GenerateMember that shows up in the property grid for all controls and components you add to a Windows Form. Wondering what it is about? It is actually a design time extender property that allows you to control whether a component added to the form is referenced by a member variable in the class or a local variable in InitializeComponent. By default, it is set to true, but if you have any components that you are not really referencing outside of InitializeComponent, you can set it to false. That way you can limit the member variables in your class to only the components you really need member variables for – just something to prevent clutter.

Same applies to Modifiers and Locked.




回答3:


Somewhat offtopic. Mostly for those, who are struggling with disabling the "Dock in Parent Container" attribute. Following override helped me:

protected override void PostFilterAttributes(IDictionary attributes)
    {
        base.PostFilterAttributes(attributes);

        var oDockingAttribute = attributes.Values.OfType<DockingAttribute>().FirstOrDefault();
        var oNoDockingAttribute = new DockingAttribute(DockingBehavior.Never);

        if (oDockingAttribute != null) attributes[oDockingAttribute.TypeId] = oNoDockingAttribute;
    }


来源:https://stackoverflow.com/questions/38582461/remove-generatemember-and-modifiers-properties-in-designer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!