WPF DataGrid的组合框结合键值对静态资源列表 [英] wpf datagrid combobox binding staticresource list of keyvalue pairs
问题描述
我希望有人能帮助我,或张贴一个链接到相关的问题,这将有答案。我看过他们大多和多数民众如何走到这一步......
所以我有3列一个DataGrid,他们两个都绑定一个数据表,第三个是一个comboboxcolumn应该被绑定到另一个。
我绑定组合框列到一个静态资源。
我不uderstand如何改造一个DataTable来,我想作为我comboboxcolumn静态资源使用键值对的列表。
公共类MyClasificators:列表< KeyValuePair<对象,对象>>
{ 公共MyClasificators()
{ this.Add(新KeyValuePair<对象,对象>(1,测试));
this.Add(新KeyValuePair<对象,对象>(2,测试1));
this.Add(新KeyValuePair&下;对象时,对象>(3,的Test2)); }
}
XAML:
<局部:MyClasificators X:键=clList>< /地方:MyClasificators>
组合框列:
< DG:DataGridTemplateColumn标题=测试>
< DG:DataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<组合框的ItemsSource ={StaticResource的clList}的DisplayMemberPath =值/>
< / DataTemplate中>
< / DG:DataGridTemplateColumn.CellTemplate>
< / DG:DataGridTemplateColumn>
现在这个工作正常,但我怎么通过这个表来MyClassificators类?如何将其转换为名单>:
DataTable的国家=新的DataTable();
country.Columns.Add(新的DataColumn(id_country的typeof(INT)));
country.Columns.Add(新的DataColumn(名,typeof运算(字符串)));
DS.Tables.Add(国家);
假设国家是你填的表,列0为id_country和列1为名:
公共MyClasificators()
{
//取得国家这点之前的DataTable 的foreach(在country.Rows的DataRow行)
{
this.Add(新KeyValuePair&下;对象时,对象>(row.ItemArray [0],row.ItemArray [1]));
}
}
通过在数据表中的所有行,这将循环获得的每行的前两个项和分别将它们添加到列表中)
I hope someone can help me or post a link to related question, that would have the answer. I have read most of them and thats how I got this far...
So I have a datagrid with 3 columns, two of them are binded to one datatable and the third one that is a comboboxcolumn should be binded to the other one.
I binded the combobox column to a static resource.
What I don't uderstand how to transform a datatable to a list of keyvalue pairs that I want to use as a static resource for my comboboxcolumn.
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>
Combobox column:
<dg:DataGridTemplateColumn Header="test">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource clList}" DisplayMemberPath="Value" / >
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
Now this works fine but how do I pass this table to the MyClassificators class and how do I convert it to list> :
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);
Assuming that "country" is your filled table, that column 0 is "id_country" and column 1 is "name" :
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]));
}
}
This will loop through all the rows in the DataTable and get the first two items of each row and add them respectively to the list ;)
这篇关于WPF DataGrid的组合框结合键值对静态资源列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!