【问题标题】:C# WPF datagrid template column with combobox having different datasourceC# WPF 数据网格模板列与组合框具有不同的数据源
【发布时间】:2020-07-24 04:41:34
【问题描述】:

我有一个带有 2 个控件的窗口。一个是组合框(cmbUnit),另一个是DataGrid (dgvProducts)

cmbUnitsList<Unit> 集合绑定,Unit 类在下面

public class Unit
{
   public int ID { get; set; }
   public string Name { get; set; }
}

注意:显示成员是名称,值成员是 ID

其中,dgvProductsList<Product> 集合绑定,Product 类在下面

public class Product
{
       public int ID { get; set; }
       public string ProductName { get; set; }
       public int UnitID { get; set; }
}

在这里,我从我的数据库表(TBL_PRODUCTS) 中选择UnitID,我也可以通过加入表来检索UnitName。现在,我只想在我的Product 类中保留UnitID

我的问题是,我的数据网格有一个列(datagridTemplateColumn),其中有一个组合框作为子项,我想通过传递UnitID 来显示其中的UnitName,就像我的组合框控件(cmbUnit) 一样

我在 datagrid 中的数据应该是

--------------------------------------------------
ID      UnitName     ProductName
--------------------------------------------------
12345    Kg         Sample Product

Kg 应显示在组合框内的位置。 如何做到这一点?帮帮我!

cmbUnit 我的 XAML 代码是

<ComboBox Grid.Column="7" Grid.Row="1" Name="cmbUnit" ></ComboBox>

我从后面的代码将数据绑定到此控件。

dgvProducts 的 XAML

<DataGrid AutoGenerateColumns="False" Grid.ColumnSpan="2"
              Name="dgvProduct"  
              Grid.Row="2">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="ID" Width="50" CanUserSort="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label VerticalContentAlignment="Center" Content="{Binding ID}"></Label>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Unit" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox IsEnabled="False"
                            Name="cmbGrUnit"
                            ItemsSource="{Binding Units}"
                            DisplayMemberPath="Name"
                            SelectedValuePath="ID"
                            SelectedValue="{Binding UnitID}"
                            ></ComboBox>
                        <!--<Label VerticalContentAlignment="Center" Content="{Binding Name}"></Label>-->
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Product Name" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label VerticalContentAlignment="Center" Content="{Binding ProductName}"></Label>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn></DataGrid.Columns>

谢谢

【问题讨论】:

  • 你能添加你的组合框的xaml代码吗?
  • @DzianisKarpuk 您好,问题已修改,XAML代码已添加,请查看!

标签: c# wpf datagrid wpf-controls


【解决方案1】:

当你有

<ComboBox Grid.Column="7" Grid.Row="1" Name="cmbUnit" ></ComboBox>

从后面的代码中设置 ItemsSource

cmbUnit.ItemsSource = this.Units

那么您可以通过以下方式将此 cmbUnit 用作绑定的源对象:

<DataGridTemplateColumn Header="Unit" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Path=ItemsSource, ElementName=cmbUnit}"
                    SelectedValuePath="ID" DisplayMemberPath="Name" SelectedValue="{Binding Path=UnitID}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

注意,DataGrid 中的 ComboBox 有自己的 DataContext,其中包含 DataGrid 的 ItemsSource 的一个元素。此外,它是 DataTemplate 的一部分,这意味着它本身不是对象,因此您不能从后面的代码直接绑定到它。

【讨论】:

  • 您的解决方案不起作用,它只是添加了一个没有值的组合框。还有{Binding Path=DataContext.Units, ElementName=window} 这里的 ElementName 只是窗口或我的窗口名称(例如:winProducts)?假设如果我使用用户控件而不是窗口,我应该为 ElementName 使用什么值?因为我把它改成了用户控件?
  • 我看到命名让你感到困惑,我已经更改了名称。请阅读binding by ElementName
  • 仍然是一个空的组合框,我也更改了 ElementName,而不是使用 DataContext.Units(这是 cmbUnit 的 itemSource,我已经提到我有两个控件。一个是组合框,另一个是 datagrid ) 我可以使用 ElementName 作为 cmbUnit 吗?如果是这样,Path 的值是多少?
  • 您能否在问题中添加您如何设置 cmbUnit 的 DataContext?
  • 全局变量 : public List&lt;Unit&gt; Units { get; set; } inside a method: this.Units = _UnitLogic.GetUnits(); this.cmbUnit.ItemsSource = this.Units; this.cmbUnit.DisplayMemberPath = "Name"; this.cmbUnit.SelectedValuePath = "ID"; 我已经用 wareframe 更新了问题,如果您有任何疑问,请查看
猜你喜欢
  • 2010-12-06
  • 2018-06-27
  • 2012-05-05
  • 2013-08-06
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多