在wpf datagrid中绑定combox [英] binding combox in wpf datagrid

查看:548
本文介绍了在wpf datagrid中绑定combox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表填充在我的viewmodel的初始化中:

I have a list that I populate in the init of my viewmodel:

ListOfEmployees = new List<EmployeeBO>(employeeRepository.GetEmployees(true, true));

我正在尝试在数据网格中获取一个组合框,以便从此列表中进行填充.

I am trying to get a combobox in a datagrid to populate from this list.

<DataGridTemplateColumn Header="U/M" MinWidth="145">
 <DataGridTemplateColumn.CellEditingTemplate>
  <DataTemplate>
   <ComboBox Name="cboUnitMeasure" 
     ItemsSource="{Binding Path=ListOfUnitMeasures}"
     DisplayMemberPath="UnitMeasureDescription" SelectedValuePath="UnitMeasureValue" 
     SelectedValue="{Binding UnitMeasureValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
     HorizontalAlignment="Left" Width="140" />
  </DataTemplate>
 </DataGridTemplateColumn.CellEditingTemplate>
 <DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding UnitMeasureDescription}" />
  </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

当dg加载时,单元格模板显示UnitMeasureDescription值,但是当我单击要编辑的单元格时,组合框中没有任何项目.另一方面,当我使用xml文件中的静态资源作为itemsource时-使用相同的属性名称-组合框包含以下各项:

When the dg loads, the cell template displays the UnitMeasureDescription value, but when I click on the cell to edit, there are no items in the combobox. On the other hand, when I use a static resource from an xml file as the itemsource-using the same property names-the combobox contains the items:

<DataGridTemplateColumn Header="U/M" MinWidth="145">
 <DataGridTemplateColumn.CellEditingTemplate>
  <DataTemplate>
   <ComboBox Name="cboUnitMeasure" 
     ItemsSource="{Binding Source={StaticResource UnitMeasureData}}"
     DisplayMemberPath="UnitMeasureDescription" SelectedValuePath="UnitMeasureValue" 
     SelectedValue="{Binding UnitMeasureValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
     HorizontalAlignment="Left" Width="140" />
  </DataTemplate>
 </DataGridTemplateColumn.CellEditingTemplate>
 <DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding UnitMeasureDescription}" />
  </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我在虚拟机中填充ListOfEmployees之后放置了一个断点,它包含项目.我还验证了DisplayMemberPath和SelectedValuePath中的属性名称正确.不知道我在这里做错了什么.

I put a breakpoint just after populating ListOfEmployees in my vm and it contains items. I also verified the property names in the DisplayMemberPath and SelectedValuePath are correct. Not sure what I am doing wrong here.

推荐答案

"ListOfUnitMeasures"是VM上的属性还是EmployeeBO的属性?好的,假设DataGrid的ItemsSource设置为List<EmployeeBO>,并且在VM上还有一个名为"ListUnitOfMeasures"的列表,这是我的解释:

Is "ListOfUnitMeasures" a property on the VM or a property of an EmployeeBO? Ok, assuming that the DataGrid's ItemsSource is set to the List<EmployeeBO> and that there's another list on the VM called "ListUnitOfMeasures", here's my explanation:

DataGrid中每一行的DataContext将等于DataGrid的ItemsSource中的元素.在您的情况下,每一行将使用EmployeeBO作为其DataContext.而且由于"ListOfUnitMeasures"不是Employee BO的属性,因此ComboBox上的绑定将不起作用,因此将不会显示任何内容.

The DataContext of each row in the DataGrid will be equal to the elements in the DataGrid's ItemsSource. In your case, each row will use an EmployeeBO as its DataContext. And since the "ListOfUnitMeasures" isn't a property of Employee BO, the Binding on the ComboBox will not work and thus won't display anything.

一种可能的解决方案是将ComboBox上的Binding更改为使用指向父DataGrid的RelativeSource,如下所示:

One possible solution is change the Binding on your ComboBox to use a RelativeSource pointing back to the parent DataGrid as follows:

<ComboBox Name="cboUnitMeasure" 
     ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.ListOfUnitMeasures}"/>

这篇关于在wpf datagrid中绑定combox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆