2010-11-25 31 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