在设计器中删除 GenerateMember 和 Modifiers 属性
Remove GenerateMember and Modifiers Properties in Designer
我创建了一个 Button 后代,我在其中隐藏了所有我不使用的属性。
我是这样做的:
[Browsable(false)]
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("", true)]
public new Boolean AllowDrop { get; set; }
大多数属性已正确隐藏且无法使用。
但是有两个属性我无法摆脱。
有没有办法同时删除设计器中的 GenerateMember 和 Modifiers?
我认为您不能删除它,因为它不是 Class 属性 而是设计时 属性 并且仅供设计人员使用:
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.
同样适用于 Modifiers
和 Locked
。
您可以创建一个新的 ControlDesigner
for your control and override its PostFilterProperties
方法。该方法允许您更改或删除属性字典中的项目。
属性字典中的键是属性的名称。尽管 Modifiers
and GenerateMember
不是您的控件的实际属性,而是设计时属性,您仍然可以通过这种方式删除它们:
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");
}
}
要在 属性 网格中隐藏属性,您可以对它们执行相同的操作,而不是覆盖或隐藏它们。
有点跑题了。主要针对那些正在努力禁用 "Dock in Parent Container" 属性的人。
以下覆盖帮助了我:
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;
}
我创建了一个 Button 后代,我在其中隐藏了所有我不使用的属性。
我是这样做的:
[Browsable(false)]
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("", true)]
public new Boolean AllowDrop { get; set; }
大多数属性已正确隐藏且无法使用。
但是有两个属性我无法摆脱。
有没有办法同时删除设计器中的 GenerateMember 和 Modifiers?
我认为您不能删除它,因为它不是 Class 属性 而是设计时 属性 并且仅供设计人员使用:
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.
同样适用于 Modifiers
和 Locked
。
您可以创建一个新的 ControlDesigner
for your control and override its PostFilterProperties
方法。该方法允许您更改或删除属性字典中的项目。
属性字典中的键是属性的名称。尽管 Modifiers
and GenerateMember
不是您的控件的实际属性,而是设计时属性,您仍然可以通过这种方式删除它们:
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");
}
}
要在 属性 网格中隐藏属性,您可以对它们执行相同的操作,而不是覆盖或隐藏它们。
有点跑题了。主要针对那些正在努力禁用 "Dock in Parent Container" 属性的人。 以下覆盖帮助了我:
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;
}