我想使用对象内部的对象的属性。 有没有办法实现这个目标?
WebProxy proxy = new WebProxy("127.0.0.1:80");
ListBox listBox = new ListBox();
listBox.DisplayMember = **"Address.Authority"**; //Note: Address.Authority is an property inside the WebProxy object
listBox.Items.Add(proxy);
感谢。
答案 0 :(得分:1)
看看this question,它基本上是在问同样的事情 - 原则在DataGridView
和ListBox
之间不会改变。简短回答:这是可能的,但令人费解。
答案 1 :(得分:0)
如何将WebProxy
子类化为例如WebProxyEx
并实现排序的IList
接口(期望实现IList或IListSource接口的对象)< / em>是使用listbox的.DataSource
属性的先决条件。如下:
class WebProxyEx : WebProxy, IList
{
private object[] _contents = new object[8];
private int _count;
public WebProxy w;
public WebProxyEx(string address)
{
_count = 0;
w = new WebProxy(address);
this.Add(w.Address.Authority);
}
...
并使用它:
ListBox lb;
public Form1()
{
InitializeComponent();
WebProxyEx w = new WebProxyEx("127.0.0.1:80");//Use your sub class
lb = new ListBox();
this.Controls.Add(lb);
lb.DataSource = w;//assign the datasource.
//lb.DisplayMember = "Address.Authority"; //Automatically gets added in the WebProxEx constructor.
}
在列表框中提供以下输出:
127.0.0.1