gpt4 book ai didi

c# - 将通用集合 List<> 绑定(bind)到属性网格

转载 作者:行者123 更新时间:2023-11-30 15:01:40 26 4
gpt4 key购买 nike

我尝试将通用集合 listContact 绑定(bind)到 propGrid 但输出与我的预期不符。我希望 listContactpropGrid 中显示为 ListBox。我该怎么做?谢谢。

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;

}

最佳答案

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

请看下面的代码。只是一个失败的例子。选择一个你最喜欢的。用于编码的来源: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
}

关于c# - 将通用集合 List<> 绑定(bind)到属性网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com