c# - 在不使用属性网格控件的情况下使用 .NET 集合编辑器

标签 c# .net collections propertygrid

我的表单上有一个 PropertyGrid。我的老板认为这很丑陋。粗野。朴实无华。

他想要一个漂亮、整洁、干净的表格。这里有一个要点:其中一个属性是我们自制对象的集合。他喜欢这个合集的合集编辑。

我知道我可以构建自己的收藏编辑器。但是是否有一种干净、简单的解决方案可以节省我几个小时的编码时间,这样我就可以在不使用属性网格的情况下直接创建和使用集合编辑器?

最佳答案

您可以从 UITypeEditor(通过 TypeDescriptor)获得此功能,但这并不简单 - 您需要设置一个 IServiceProvider,一个 IWindowsFormsEditorService,最好是一个 ITypeDescriptorContext - 相当多的废话。如果您不熟悉这些工具,手动操作可能更简单。

或者 - 看看 SmartPropertyGrid.NETPropertyGrid 的替代方案。


更新:这是一个工作示例...绝对不平凡,但可以随意窃取代码。它仅适用于模态编辑器,不适用于下拉菜单。它也不是“关注点分离”的一个很好的例子。 MyHelper 类很有趣。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
class Foo
{
    public Foo() { Bars = new List<Bar>(); }
    public List<Bar> Bars { get; private set; }
}
class Bar
{
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        Foo foo = new Foo();
        Bar bar = new Bar();
        bar.Name = "Fred";
        bar.DateOfBirth = DateTime.Today;
        foo.Bars.Add(bar);
        Application.EnableVisualStyles();
        using(Form form = new Form())
        using (Button btn = new Button())
        {
            form.Controls.Add(btn);
            btn.Text = "Edit";
            btn.Click += delegate
            {
                MyHelper.EditValue(form, foo, "Bars");
            };
            Application.Run(form);
        }
    }
}

class MyHelper : IWindowsFormsEditorService, IServiceProvider, ITypeDescriptorContext
{
    public static void EditValue(IWin32Window owner, object component, string propertyName) {
        PropertyDescriptor prop = TypeDescriptor.GetProperties(component)[propertyName];
        if(prop == null) throw new ArgumentException("propertyName");
        UITypeEditor editor = (UITypeEditor) prop.GetEditor(typeof(UITypeEditor));
        MyHelper ctx = new MyHelper(owner, component, prop);
        if(editor != null && editor.GetEditStyle(ctx) == UITypeEditorEditStyle.Modal)
        {
            object value = prop.GetValue(component);
            value = editor.EditValue(ctx, ctx, value);
            if (!prop.IsReadOnly)
            {
                prop.SetValue(component, value);
            }
        }
    }
    private readonly IWin32Window owner;
    private readonly object component;
    private readonly PropertyDescriptor property;
    private MyHelper(IWin32Window owner, object component, PropertyDescriptor property)
    {
        this.owner = owner;
        this.component = component;
        this.property = property;
    }
    #region IWindowsFormsEditorService Members

    public void CloseDropDown()
    {
        throw new NotImplementedException();
    }

    public void DropDownControl(System.Windows.Forms.Control control)
    {
        throw new NotImplementedException();
    }

    public System.Windows.Forms.DialogResult ShowDialog(System.Windows.Forms.Form dialog)
    {
        return dialog.ShowDialog(owner);
    }

    #endregion

    #region IServiceProvider Members

    public object GetService(Type serviceType)
    {
        return serviceType == typeof(IWindowsFormsEditorService) ? this : null;
    }

    #endregion

    #region ITypeDescriptorContext Members

    IContainer ITypeDescriptorContext.Container
    {
        get { return null; }
    }

    object ITypeDescriptorContext.Instance
    {
        get { return component; }
    }

    void ITypeDescriptorContext.OnComponentChanged()
    {}

    bool ITypeDescriptorContext.OnComponentChanging()
    {
        return true;
    }

    PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor
    {
        get { return property; }
    }

    #endregion
}

关于c# - 在不使用属性网格控件的情况下使用 .NET 集合编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/473506/

相关文章:

c# - UWP 和 WPF 数据库入门 : Roadmap needed

c# - 当其中一个包含非法字符时,如何获取 ZipArchive 的有效条目?

c# - 为什么 Quartz.Net 似乎要求我的项目以完整的 .NET Framework 4 为目标?

c# - 在调试器中跳过表达式主体属性

c# - .Net MVC认证方式

.net - Asp.net MVC json或Json.net?

java - 我们可以用Java实现C++风格的列表吗?

Java:将数组转换为列表异常

c# - OpenSearch .net (C#) 库 : which one?

collections - 将 int 列表折叠为 kotlin 中的范围列表