简体   繁体   English

绑定datagrid comboboxcolumn WPF

[英]Binding datagrid comboboxcolumn WPF

I have a problem when I try to bind my combobox col. 尝试绑定组合框col时出现问题。

I have 2 DataSet tables and i want the combo box to show the values of 1 DataSet table but the selected index will be taken from the other DataSet. 我有2个DataSet表,我希望组合框显示1个DataSet表的值,但是所选索引将从其他DataSet中获取。

Example : 范例:

1.Material DataSet with Id col and UnitID col that represet the unit this material weight is calculated. 1.具有ID col和UnitID col的材料数据集可重新设置计算此物料重量的单位。

2.Units DataSet With Id col and Name 2.使用ID col和Names设置DataSet

The datagrid will show the Material dataset details ( id as textbox col and unitID as comboBox col) datagrid将显示Material数据集的详细信息(id为文本框col,unitID为comboBox col)

I want my datagrid combobox col to show all Units Names in the combobox option and the selected item will be the one with the index equals to the material UnitID. 我希望我的datagrid combobox col在combobox选项中显示所有单位名称,并且所选项目将是索引等于材料UnitID的项目。

I succeeded to show all my units names as the combobox options with : 我成功显示了所有单位名称作为组合框选项,方法是:

cmb.ItemsSource = DS.Units.ToList

cmb.DisplayMemberPath = "EnglishName"

But was unable to specify the SelectedValuePath and SelectedValueBinding at the right way. 但是无法以正确的方式指定SelectedValuePath和SelectedValueBinding。

One data sources can be used for multiple combo boxes however two data sources can't bind to one control. 一个数据源可以用于多个组合框,但是两个数据源不能绑定到一个控件。 Instead we can use LINQ to combine these data sources and assign resulted data source to combo box. 相反,我们可以使用LINQ组合这些数据源并将结果数据源分配给组合框。

DataTable material= ds.Tables["Material"];
DataTable units= ds.Tables["Units"];

var query =
    from m in material.AsEnumerable()
    join u in units.AsEnumerable()
    on m.Field<int>("Column1") equals
        u.Field<int>("Column1")
select new
    {
        Text =
            u.Field<string>("EnglishName"),
        Value =
            m.Field<int>("MaterialID")
    };

cmb.ItemsSource = query.ToList();

cmb.DisplayMemberPath = "Text";
cmb.SelectedValuePath = "Value"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM