2010-05-25 26 views
2

我有一個用戶控件...並且使用此用戶控件的基頁具有將由用戶控件使用的數據集。我們是否可以將動態數據(數據集或列表)綁定到WPF中的控件

我的數據集是動態的...意味着它可以有不同數量的列,這取決於我的用戶控件實現的頁面。有沒有在WPF中,我可以用來綁定這個數據集(不知道列信息)的任何控制...我的意思是類似於2.0中的datagrid/gridview?

回答

5

看看WPF toolkit,它包含一個符合您的要求的網格。

此工具包由Microsoft構建,屬於Microsoft Shared Source Initiative(請參閱我提供的鏈接)。

雖然這不是我的微軟支持的,所以如果你有一個bug,你可以使用他們的論壇,但不能打電話給MS支持。

如果你確實想自己做,你可以寫一些代碼,它需要一個List<T>,你得到泛型類型,獲取屬性,迭代它們,寫列標題,迭代所有項目列表並寫入所有屬性。

我寫了這個代碼,而回到列表寫HTML表格,我希望這是有用的:?

public void PrintList<T>(List<T> list) 
{ 
    if(null!=list.FirstOrDefault()) 
    { 
     Type t = typeof(list[0]); 
     PropertyInfo[] properties = t.GetProperties(); 

     // properties = list of all properties. 

     print("<table><tr>"); 

     foreach(var property in properties) 
     { 
      // print property title 
      print(string.Format("<th>{0}</th>", property.Name)); 
     } 


     print("</tr>"); 

     foreach(var item in list) 
     { 
      print("<tr>"); 
      foreach(var property in properties) 
      { 
       var propertyValue = t.InvokeMember(property.Name, BindingFlags.GetProperty, null, item, new object[] {}); 
       print(string.Format("<td>{0}</td>", propertyValue)); 
      } 

      print("</tr>"); 
     } 

     print("</table>"); 
    } 
} 
+0

是微軟WPF工具包。因爲我的客戶是verymuch嚴格ABT「不using-第三方控制「:( – Relativity 2010-05-25 06:33:47

相關問題