2010-11-25 90 views
1

我想使用对象内部的对象的属性。 有没有办法做到这一点?使用对象属性作为ListBox中的DisplayMember

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); 

谢谢。

回答

1

看看this question,它本质上要求相同的事情 - 原理不会在DataGridViewListBox之间变化。简而言之:这是可能的,但是令人费解。

+0

我用了萨韦的做法该OP并创建了一个包含我的WebProxy对象和一个str的新对象这给了我的代理权。感谢您的回答。 – 2010-11-25 17:21:17

0

你怎么样继承WebProxy到,例如,WebProxyEx并实现IList接口,排序(预计实现IList或IListSource接口的对象)是一个先决条件,使用列表框的.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

+0

感谢您的回答。我不知道是否有必要实施IList,因为我已经有一个列表作为我的数据源。 – 2010-11-25 17:17:59