如何在“ CollectionEditor”对话框中为属性启用默认值 [英] How to enable Default Values for properties in a 'CollectionEditor' dialog
问题描述
请先阅读整个问题,以了解位置,我将能够重置属性的默认值。
Please read entire question first to understand where I would have the ability to reset the default value of a property.
在定义可以可视化设计的自定义类时,可以使用以下模式来实现集合编辑器来修改列表,数组,集合等属性:
When defining a custom class that can be visually designed, one can implement a collections editor to modify properties which are lists, arrays, collections, using the following pattern:
[Editor(typeof(CollectionEditor), typeof(UITypeEditor)),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ElementCollection Elements
{
get;
}
编辑元素
此类的属性现在将启动 CollectionEditor
对话框,左侧是成员列表,而在列表中是 PropertyGrid
Editing the Elements
property of this class will now launch a CollectionEditor
dialog, with a list of members on the left and a PropertyGrid
on the right.
问题是,似乎此属性网格的上下文菜单已禁用。因此,我无法右键单击属性以尽管已定义 [DefaultValue]
属性,但仍将其值重置为默认值。
The problem is, it appears that context menus are disabled for this property grid. Therefore, I cannot right click on a property to 'reset' its value to default, despite having a [DefaultValue]
attribute defined.
但是,可以识别 DefaultValue
属性,因为该属性未序列化(并且正确显示在
Yet, the DefaultValue
attribute is recognized, because the property is not serialized (and correctly displayed in unbolded text within the grid).
我想知道如何在 PropertyGrid $ c上启用此上下文菜单 $ c>来自
CollectionEditor
对话框:
I would like to know how to enable this context menu on the PropertyGrid
from the CollectionEditor
dialog:
或者,可以通过任何方式(热键,等)实施以能够重置这些收集项属性。
or alternatively, any way (hot key, etc.) that can be implemented to be able to reset these collection item properties.
推荐答案
您可以继承 CollectionEditor
类,然后覆盖 CreateCollectionForm
方法,在集合编辑器表单中找到属性网格,然后注册包含属性网格 Reset 菜单项的 ContextMenuStrip
,然后使用< a href = https://msdn.microsoft.com/zh-cn/library/system.windows.forms.propertygrid.resetselectedproperty(v=vs.110).aspx rel = nofollow noreferrer> ResetSelectedProperty
:
You can create your own collection editor inheriting CollectionEditor
class and then override CreateCollectionForm
method, find property grid in the collection editor form and then register a ContextMenuStrip
containing a Reset menu item for property grid, then reset the property using ResetSelectedProperty
:
public class MyCollectionEditor : CollectionEditor
{
public MyCollectionEditor() : base(typeof(Collection<MyElement>)) { }
protected override CollectionForm CreateCollectionForm()
{
var form = base.CreateCollectionForm();
var grid = form.Controls.Find("propertyBrowser", true).First() as PropertyGrid;
var menu = new ContextMenuStrip();
menu.Items.Add("Reset", null, (s, e) => { grid.ResetSelectedProperty(); });
//Enable or disable Reset menu based on selected property
menu.Opening += (s, e) =>
{
if (grid.SelectedGridItem != null && grid.SelectedObject != null &&
grid.SelectedGridItem.PropertyDescriptor.CanResetValue(null))
menu.Items[0].Enabled = true;
else
menu.Items[0].Enabled = false;
};
grid.ContextMenuStrip = menu;
return form;
}
}
并以此方式装饰收藏属性:
And decorate your collection property this way:
[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Collection<MyElement> MyElements { get; private set; }
按照这种方法,您可以简单地添加分隔符,命令和描述菜单。
Following this approach you can simply add a separator, commands and description menus.
这篇关于如何在“ CollectionEditor”对话框中为属性启用默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!