2013-05-18 53 views
0

我希望有人可以幫助我或發佈一個鏈接到相關的問題,這將有答案。我已閱讀他們大多數,這就是我如何得到這麼遠...wpf datagrid組合框綁定staticresource keyvalue對列表

所以我有一個3列datagrid,其中兩個綁定到一個數據表和第三個是一個comboboxcolumn應綁定到另一個。

我把combobox列綁定到一個靜態資源。

我不理解如何將數據錶轉換爲我想用作我的combobox列的靜態資源的keyvalue對列表。

public class MyClasificators:List<KeyValuePair<object, object>> 
{ 

    public MyClasificators() 
    { 

     this.Add(new KeyValuePair<object, object>(1, "Test")); 
     this.Add(new KeyValuePair<object,object>(2, "Test1")); 
     this.Add(new KeyValuePair<object, object>(3, "Test2")); 

    } 
} 

XAML:

<local:MyClasificators x:Key="clList"></local:MyClasificators> 

組合框柱:

<dg:DataGridTemplateColumn Header="test"> 
    <dg:DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
     <ComboBox ItemsSource="{StaticResource clList}" DisplayMemberPath="Value"/> 
     </DataTemplate> 
     </dg:DataGridTemplateColumn.CellTemplate> 
</dg:DataGridTemplateColumn> 

現在這工作正常,但我怎麼通過這個表來MyClassificators類,我如何將其轉換爲列表> :

DataTable country = new DataTable(); 
      country.Columns.Add(new DataColumn("id_country", typeof(int))); 
      country.Columns.Add(new DataColumn("name", typeof(string))); 
      DS.Tables.Add(country); 
+0

因此,您想將MyClassificator的內容放入DataTable中? –

+0

沒有其他的方式。我想把Datatable的所有條目都放到MyClassificator中。 – acosta

回答

0

假設「國家」是你填的表,該列0是「id_country」和列1爲「名」:通過在數據表中的所有行和

public MyClasificators() 
{ 
    //Acquire "country" DataTable before this point 

    foreach (DataRow row in country.Rows) 
    { 
     this.Add(new KeyValuePair<object, object>(row.ItemArray[0], row.ItemArray[1])); 
    } 
} 

這將循環獲取每個的前兩個項目並將它們分別添加到列表中;)

+1

爲什麼感謝你,善良的先生!我必須承認,我從來不知道這裏的人有多麼友善和樂於助人。 – acosta

+0

我仍然有執行此問題,雖然。 'code'公共類MyClasificators:列表> { 公共無效_MyClasificators(DataTable的國家) { 的foreach(DataRow的行country.Rows) { this.Add(新KeyValuePair <對象,object>(row.ItemArray [0],row.ItemArray [1]));我們有'代碼'public void callMyClassificators() { } MyClasificators clasif = new MyClasificators(); clasif._MyClasificators(country); }'code' – acosta

+0

你有什麼樣的問題? –