Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定通用集合列表<&燃气轮机;到属性网格_C#_Winforms_Binding_Collections_Propertygrid - Fatal编程技术网

C# 绑定通用集合列表<&燃气轮机;到属性网格

C# 绑定通用集合列表<&燃气轮机;到属性网格,c#,winforms,binding,collections,propertygrid,C#,Winforms,Binding,Collections,Propertygrid,我尝试将generic collectionlistContact绑定到propGrid,但输出与我预期的不匹配。我希望listContact像ListBox一样显示在propGrid中。我该怎么做?多谢各位 class Contact { public string Name { get; set; } public string Address { get; set; } } PropertyGrid propGrid = new PropertyGrid(); List&

我尝试将generic collectionlistContact绑定到propGrid,但输出与我预期的不匹配。我希望listContactListBox一样显示在propGrid中。我该怎么做?多谢各位

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;

}
class联系人
{
公共字符串名称{get;set;}
公共字符串地址{get;set;}
}
PropertyGrid propGrid=新的PropertyGrid();
List listContact=新列表();
私有void Form1\u加载(对象发送方、事件参数e)
{
listContact.Clear();
联系人newContact=null;
newContact=新联系人();
newContact.Name=“diana”;
newContact.Address=“en”;
listContact.Add(newContact);
newContact=null;
newContact=新联系人();
newContact.Name=“maxim”;
newContact.Address=“cand”;
listContact.Add(newContact);
propGrid.SelectedObject=listContact;
this.Controls.Add(propGrid);
propGrid.Dock=DockStyle.Fill;
}

您必须扩展类才能使用
ExpandableObjectConverter
。这使得可解析的事件发生

请参阅下面的代码。只是一个例子。挑一个你最喜欢的。 用于编写以下代码的源代码:

[TypeConverter(typeof(Contact))]
[DescriptionAttribute(“展开以查看应用程序的拼写选项”。)]
类联系人:ExpandableObjectConverter
{
[DefaultValueAttribute(“联系人姓名”)]
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共覆盖布尔CanConvertTo(ITypeDescriptorContext上下文,类型destinationType)
{
如果(destinationType==typeof(联系人))
{
返回true;
}
返回base.CanConvertTo(上下文,destinationType);
}
公共重写对象转换为(ITypeDescriptorContext上下文,System.Globalization.CultureInfo区域性,对象值,类型destinationType)
{
if(destinationType==typeof(System.String)&&值为Contact)
{
触点触点=作为触点的值;
返回string.Format(“名称:{0}-地址:{1}”,contact.Name,contact.Address);
}
返回base.ConvertTo(上下文、区域性、值、destinationType);
}
}
[类型转换器(类型(触点2))]
[DescriptionAttribute(“展开以查看应用程序的拼写选项”。)]
类Contact2:ExpandableObjectConverter
{
私人联络1;
公众联络1
{
得到
{
返回contato1;
}
设置
{
contato1=值;
}
}
私人联系contato3;
公众联络小组3
{
得到
{
返回contato3;
}
设置
{
contato3=数值;
}
}
公众联络2()
{
this.contato1=新联系人()
{
Address=“Add1”,
Name=“Name1”
};
this.contato3=新联系人()
{
Address=“Add3”,
Name=“Name3”
};
}
}
公共部分类表单2:表单
{
PropertiesList listContact=新的PropertiesList();
//List listContact=新列表();
公共表格2()
{
初始化组件();
}
私有void Form2_加载(对象发送方、事件参数e)
{
listContact.Clear();
联系人newContact=null;
newContact=新联系人();
newContact.Name=“diana”;
newContact.Address=“en”;
listContact.Add(newContact);
newContact=null;
newContact=新联系人();
newContact.Name=“maxim”;
newContact.Address=“cand”;
listContact.Add(newContact);
propGrid.AllowDrop=true;
object[]itens=新对象[2];
itens[0]=listContact;
itens[1]=新触点;
propGrid.SelectedObject=listContact;
this.Controls.Add(propGrid);
propGrid.Dock=DockStyle.Fill;
}
}
[类型转换器(类型(触点))]
类属性列表:ExpandableObjectConverter,其中T:联系人
{
私有列表innerList=新列表();
公开名单名称
{
得到
{
列表值返回=null;
如果(innerList.Count>0)
{
VALUESRETURN=新列表();
for(int i=0;i0)
{
VALUESRETURN=新列表();
for(int i=0;i
地址是否没有
[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
}