回答我自己的问题——经过多次试验;检查强积金的项目源代码;和一些谷歌搜索;我发现必须使用Editor属性装饰作为属性公开的类,并创建一个从UITypeEditor派生的自定义编辑器。通常在WinForms
其中找到可以在其他地方使用,包括它看起来的自定义项目系统。
例如,这是在我们的项目系统中代表“模型”文件的文件节点。(一些代码,虽然这里不重要,但已被省略)。下面Plugins
的属性是我想要一个[...]按钮的属性。
[ComVisible(true)]
[CLSCompliant(false)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("xx-xx-xx-xx-xx")]
internal class ModelFileNodeProperties : MbtFileNodeProperties
{
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="ModelFileNodeProperties" /> class.
/// </summary>
/// <param name="node">The node.</param>
public ModelFileNodeProperties(HierarchyNode node) : base(node)
{
Plugins = new Plugins(node); // Our "model" node has a concept of "plug-ins"
}
#endregion
#region Properties
[LocDisplayName("Plugins")]
public Plugins Plugins { get; set; } // <----- I wan't a [...] button for this property
...
}
...这是代表我们模型文件的“插件”的类。注意Editor
属性:
[Editor(typeof (PluginsEditor), typeof (UITypeEditor))] // <---- MPF will spot this and expose the editor to Visual Studio
internal class Plugins
{
#region Fields
private readonly HierarchyNode _node;
public Plugins(HierarchyNode node) { _node = node; }
// this prevents the object type being displayed in the Property window
public override string ToString() { return "No plug-ins installed"; }
public string Something { get; set; }
public string OrOther { get; set; }
[Microsoft.VisualStudio.Project.LocDisplayName("Things")]
public List<int> Things { get; set; }
#endregion
...
}
这是用于呈现和修改Plugins
类型的编辑器。
internal class PluginsEditor : UITypeEditor
{
public PluginsEditor()
{
}
#region Methods
/// <summary>
/// Edits the specified object's value using the editor style indicated by the
/// <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle" /> method.
/// </summary>
/// <returns>
/// The new value of the object. If the value of the object has not changed, this should return the same object it was
/// passed.
/// </returns>
/// <param name="context">
/// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that can be used to gain
/// additional context information.
/// </param>
/// <param name="provider">An <see cref="T:System.IServiceProvider" /> that this editor can use to obtain services. </param>
/// <param name="value">The object to edit. </param>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
var form = new PluginsForm();
form.ShowDialog(); // we want to show a modal dialog
return value;
}
/// <summary>
/// Gets the editor style used by the
/// <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)" /> method.
/// </summary>
/// <returns>
/// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle" /> value that indicates the style of editor used by the
/// <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)" /> method. If the
/// <see cref="T:System.Drawing.Design.UITypeEditor" /> does not support this method, then
/// <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle" /> will return
/// <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None" />.
/// </returns>
/// <param name="context">
/// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that can be used to gain
/// additional context information.
/// </param>
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal; // we don't want a drop-down but rather a modal dialog
}
#endregion
}