2013-04-26 69 views
2

我希望有某個地方能夠幫助我解決這個問題。我如何迭代通過Compact Framework中的BindingSource項目c#

我正在爲windows ce移動設備創建自定義控件。我創建了一個屬性,以便用戶可以將任何源代碼綁定到我的控件。我需要遍歷源代碼並獲取顯示成員路徑的項目值。

我聲明我的來源,然後創建屬性。在構造中,如果源發生更改,我會註冊一個事件,以便我可以再次開始繪製對象。這是我第一次從這個角度處理綁定源碼。我錯過了什麼或使用它錯了嗎?這裏是一些代碼片段

private BindingSource _bindingCollection; 

public object DataSource 
     { 
      set { _bindingCollection.DataSource = value; } 
     } 
public string DisplayMemberPath { get; set; } 

ctor() 
{ 
_bindingCollection.DataSourceChanged += _bindingCollection_DataSourceChanged; 
} 


void _bindingCollection_DataSourceChanged(object sender, EventArgs e) 

     { 
      foreach (var item in _bindingCollection) 
      { 
       //I am stuck here on getting the value from the item that is 
       // specified by DisplayMemberPath to paint my objects on the control 
       //I can only paint on the control and cannot add any controls to it. 
       //So here a method is called with the value as a parameter and the 
       //Graphics object will draw the string 
      } 
     } 

謝謝先進。

問候 裏安

編輯:

哥們,我知道您使用paint方法來作畫,這是我想要做的一個例子。我從頭開始創建所有的東西,整個控件是從代碼中提取的,我覆蓋了所有需要的事件。也許在DisplayMemberPath中設置_bindingCollection.DataMember?那麼當迭代源代碼時,它會給出這個項目嗎?現在測試併發布答案,如果它有效。請不要更多的評論或回答告訴我使用油漆或類似的東西。專注於從集合中獲得顯示成員價值的問題:)

編輯:找到答案 好吧,對不起,這實際上是一個非常愚蠢的問題,我在忙於很多事情時都會問。這實際上很容易獲得財產價值。

 for (int index = 0; index < _bindingCollection.Count; index++) 
     { 
      var propertyValue = _bindingCollection[index].GetType().GetProperty(DisplayMemberPath).GetValue(_bindingCollection[index], null); 
      // Can do anthing now with the value here or just create a 
      //method of returning this value with this line of code on top. 

     } 

如果有人需要刪除它,可以刪除該問題。如果有其他人急於解決這個問題並且不知道如何得到它,我會把它留在這裏。這些只是代碼片段,而不是可以使用的方法。

+0

你不應該在DataSourceChanged中畫東西,你需要使用Paint事件。 – 2013-04-26 06:29:47

回答

1

我只是將問題標記爲已回答。閱讀我的編輯,看看我愚蠢的思想錯誤,爲什麼它很容易。

我會離開這個崗位的人,可能會去和我一樣思考,並非常努力,除非有人覺得這個問題必須刪除,你可以繼續前進。