Utilizzo dell'editor di raccolte .NET senza utilizzare un controllo griglia delle proprietà

StackOverflow https://stackoverflow.com/questions/473506

Domanda

Ho un PropertyGrid nel mio modulo. Il mio capo pensa che sia brutto. Rozzo. Sofisticato.

Vuole una forma bella, ordinata, pulita. Ecco il trucco: una delle proprietà è una raccolta dei nostri oggetti coltivati ??in casa. Gli piace l'editor della raccolta per questa raccolta.

So di poter creare il mio editor di raccolte. Ma esiste una soluzione semplice e pulita per salvarmi qualche ora di programmazione, in modo tale da poter creare e utilizzare direttamente un editor Collection senza utilizzare la griglia delle proprietà?

È stato utile?

Soluzione

È possibile ottenere questa funzionalità da UITypeEditor (tramite TypeDescriptor ), ma non è banale: è necessario impostare un IServiceProvider , un IWindowsFormsEditorService e idealmente un ITypeDescriptorContext - un bel po 'di faff. Potrebbe essere più semplice farlo a mano se non si ha familiarità con tali strumenti.

In alternativa, dai un'occhiata a SmartPropertyGrid.NET , un'alternativa a PropertyGrid .


Aggiornamento: ecco un esempio funzionante ... sicuramente non banale, ma sentiti libero di rubare il codice. Funziona solo con editor modali, non con menu a discesa. Inoltre, non è un ottimo esempio di "separazione delle preoccupazioni". La classe MyHelper è quella interessante.

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
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top