结合泛型集合列表与LT;>到属性网格 [英] Binding generic collection List<> to property grid

查看:81
本文介绍了结合泛型集合列表与LT;>到属性网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试结合泛型集合 listContact propGrid 但产量不匹配我的预期。我想 listContact propGrid ,以显示例如的ListBox 。我该怎么做?谢谢。

 类联系
{
    公共字符串名称{;组; }
    公共字符串地址{搞定;组; }
}PropertyGrid的propGrid =新的PropertyGrid();
清单<联系与GT; listContact =新的List<联系与GT;();私人无效Form1_Load的(对象发件人,EventArgs的发送)
{
    listContact.Clear();
    联系newContact = NULL;    newContact =新的联系人();
    newContact.Name =戴安娜;
    newContact.Address =EN;
    listContact.Add(newContact);    newContact = NULL;
    newContact =新的联系人();
    newContact.Name =格言
    newContact.Address =C和;
    listContact.Add(newContact);    propGrid.SelectedObject = listContact;
    this.Controls.Add(propGrid);
    propGrid.Dock = DockStyle.Fill;}


解决方案

您必须扩展您的类使用 ExpandableObjectConverter 。这使得解析的发生。

请参阅下面的code。只是下跌的例子。选择您最喜欢的之一。
用于code来源: MSDN

  [的TypeConverter(typeof运算(联系))]
[DescriptionAttribute(展开以查看应用程序的拼写选项。)
类联系人:ExpandableObjectConverter
{
    [DefaultValueAttribute(联系人姓名)]
    公共字符串名称{;组; }
    公共字符串地址{搞定;组; }    公众覆盖布尔CanConvertTo(ITypeDescriptorContext背景下,类型destinationType)
    {
        如果(destinationType == typeof运算(联系))
        {
            返回true;
        }
        返回base.CanConvertTo(背景下,destinationType);
    }    公众覆盖对象的ConvertTo(ITypeDescriptorContext背景下,System.Globalization.CultureInfo文化,对象的值,类型destinationType)
    {
        如果(destinationType == typeof运算(System.String)及和放大器;价值联系)
        {
            联系方式联系方式=价值联系人;            返回的String.Format(名称:{0} - 地址:{1},contact.Name,contact.Address);
        }
        返回base.ConvertTo(上下文,文化,价值,destinationType);
    }
}[的TypeConverter(typeof运算(Contact2))]
[DescriptionAttribute(展开以查看应用程序的拼写选项。)
类Contact2:ExpandableObjectConverter
{
    私人联系contato1;
    公开联系Contato1
    {
        得到
        {
            返回contato1;
        }
        组
        {
            contato1 =价值;
        }
    }    私人联系contato3;
    公开联系Contato3
    {
        得到
        {
            返回contato3;
        }
        组
        {
            contato3 =价值;
        }
    }    公共Contact2()
    {
        this.contato1 =新的联系人()
        {
            地址=ADD1
            NAME =名称1
        };
        this.contato3 =新的联系人()
        {
            地址=ADD3
            NAME =NAME3
        };
    }
}公共部分类窗体2:表
{
    PropertiesList<联系与GT; listContact =新PropertiesList<联系与GT;();
    //列表<联系与GT; listContact =新的List<联系与GT;();    公共窗体2()
    {
        的InitializeComponent();
    }    私人无效Form2_Load(对象发件人,EventArgs的发送)
    {
        listContact.Clear();
        联系newContact = NULL;        newContact =新的联系人();
        newContact.Name =戴安娜;
        newContact.Address =EN;
        listContact.Add(newContact);        newContact = NULL;
        newContact =新的联系人();
        newContact.Name =格言
        newContact.Address =C和;
        listContact.Add(newContact);        propGrid.AllowDrop = TRUE;        [对象] itens =新对象[2];
        itens [0] = listContact;
        itens [1] = newContact;
        propGrid.SelectedObject = listContact;
        this.Controls.Add(propGrid);
        propGrid.Dock = DockStyle.Fill;
    }
}[的TypeConverter(typeof运算(联系))]
类PropertiesList< T> :ExpandableObjectConverter其中T:联系
{
    私人列表< T> innerList =新的List< T>();    公开名单<串GT;名称
    {
        得到
        {
            清单<串GT; values​​Returned = NULL;
            如果(innerList.Count大于0)
            {
                values​​Returned =新的List<串GT;();
                的for(int i = 0; I< innerList.Count;我++)
                {
                    values​​Returned.Add(innerList [I] .Name点);
                }            }
            返回values​​Returned;
        }
    }    公开名单< T>项目
    {
        得到
        {
            清单< T> values​​Returned = NULL;
            如果(innerList.Count大于0)
            {
                values​​Returned =新的List< T>();
                的for(int i = 0; I< innerList.Count;我++)
                {
                    values​​Returned.Add(innerList [I]);
                }            }
            返回values​​Returned;
        }
    }    众彩GetColors {搞定;组; }    公共Contact2 Contato22
    {
        得到
        {
            返回新Contact2();
        }
    }    公众覆盖布尔CanConvertTo(ITypeDescriptorContext背景下,类型destinationType)
    {
        如果(destinationType == typeof运算(联系))
        {
            返回true;
        }
        返回base.CanConvertTo(背景下,destinationType);
    }    公众覆盖对象的ConvertTo(ITypeDescriptorContext背景下,System.Globalization.CultureInfo文化,对象的值,类型destinationType)
    {
        如果(destinationType == typeof运算(System.String)及和放大器;价值联系)
        {
            联系方式联系方式=价值联系人;            返回的String.Format(名称:{0} - 地址:{1},contact.Name,contact.Address);
        }
        返回base.ConvertTo(上下文,文化,价值,destinationType);
    }    #区域模拟名单
    公共无效添加(T项)
    {
        innerList.Add(项目);
    }    公共无效清除()
    {
        innerList.Clear();
    }
    #endregion
}

I try binding generic collection listContact to propGrid but output does not match what I expected. I want listContact to be shown like ListBox in propGrid. How do I do it? Thank you.

class Contact
{
    public string Name { get; set; }
    public string Address { get; set; }
}

PropertyGrid propGrid = new PropertyGrid();
List<Contact> listContact   = new List<Contact>();

private void Form1_Load(object sender, EventArgs e)
{
    listContact.Clear();
    Contact newContact = null;

    newContact = new Contact();
    newContact.Name = "diana";
    newContact.Address = "en";
    listContact.Add(newContact);

    newContact = null;
    newContact = new Contact();
    newContact.Name = "maxim";
    newContact.Address = "cand";
    listContact.Add(newContact);

    propGrid.SelectedObject = listContact;
    this.Controls.Add(propGrid);
    propGrid.Dock = DockStyle.Fill;

}

解决方案

You have to extend you class to use ExpandableObjectConverter. This makes the parseable happen.

See the below code. Just a fell examples. Pick the one you like the most. The source used to code : MSDN

[TypeConverter(typeof(Contact))]
[DescriptionAttribute("Expand to see the spelling options for the application.")]
class Contact : ExpandableObjectConverter
{
    [DefaultValueAttribute("Contact Name")]
    public string Name { get; set; }
    public string Address { get; set; }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(Contact))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(System.String) && value is Contact)
        {
            Contact contact = value as Contact;

            return string.Format("Name: {0} - Address: {1}", contact.Name, contact.Address);
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

[TypeConverter(typeof(Contact2))]
[DescriptionAttribute("Expand to see the spelling options for the application.")]
class Contact2 : ExpandableObjectConverter
{
    private Contact contato1;
    public Contact Contato1
    {
        get
        {
            return contato1;
        }
        set
        {
            contato1 = value;
        }
    }

    private Contact contato3;
    public Contact Contato3
    {
        get
        {
            return contato3;
        }
        set
        {
            contato3 = value;
        }
    }

    public Contact2()
    {
        this.contato1 = new Contact()
        {
            Address = "Add1",
            Name = "Name1"
        };
        this.contato3 = new Contact()
        {
            Address = "Add3",
            Name = "Name3"
        };
    }
}

public partial class Form2 : Form
{
    PropertiesList<Contact> listContact = new PropertiesList<Contact>();
    //List<Contact> listContact = new List<Contact>();

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        listContact.Clear();
        Contact newContact = null;

        newContact = new Contact();
        newContact.Name = "diana";
        newContact.Address = "en";
        listContact.Add(newContact);

        newContact = null;
        newContact = new Contact();
        newContact.Name = "maxim";
        newContact.Address = "cand";
        listContact.Add(newContact);

        propGrid.AllowDrop = true;

        object[] itens = new object[2];
        itens[0] = listContact;
        itens[1] = newContact;
        propGrid.SelectedObject = listContact;
        this.Controls.Add(propGrid);
        propGrid.Dock = DockStyle.Fill;
    }
}

[TypeConverter(typeof(Contact))]
class PropertiesList<T> : ExpandableObjectConverter where T : Contact
{
    private List<T> innerList = new List<T>();

    public List<string> Names
    {
        get
        {
            List<string> valuesReturned = null;
            if (innerList.Count > 0)
            {
                valuesReturned = new List<string>();
                for (int i = 0; i < innerList.Count; i++)
                {
                    valuesReturned.Add(innerList[i].Name);
                }

            }
            return valuesReturned;
        }
    }

    public List<T> Item
    {
        get
        {
            List<T> valuesReturned = null;
            if (innerList.Count > 0)
            {
                valuesReturned = new List<T>();
                for (int i = 0; i < innerList.Count; i++)
                {
                    valuesReturned.Add(innerList[i]);
                }

            }
            return valuesReturned;
        }
    }

    public Color GetColors { get; set; }

    public Contact2 Contato22
    {
        get
        {
            return new Contact2();
        }
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(Contact))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(System.String) && value is Contact)
        {
            Contact contact = value as Contact;

            return string.Format("Name: {0} - Address: {1}", contact.Name, contact.Address);
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

    #region Simulate List
    public void Add(T item)
    {
        innerList.Add(item);
    }

    public void Clear()
    {
        innerList.Clear();
    }
    #endregion
}

这篇关于结合泛型集合列表与LT;&GT;到属性网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆