[英]How to get the SelectedItem property from a ComboBox column in a DataGrid wpf
最近,我们开始在工作中使用WPF。 现在,我想从包含项目角色的对象列表(DataGrid ItemSource),应该执行此工作的员工以及也可以执行该工作的员工列表中创建一个DataGrid。 让我们将此列表称为“ MainList”。 在该DataGrid中,是一个ComboBox列,该列使用另一个对象列表作为ItemSource,您可以在其中更改作业的员工。 我将此列表称为“ ChildList”。 该列表包含在MainList中(正如已经提到的那样),我通过使用正确的BindingPath对其进行绑定。 到现在为止还挺好。 现在,我必须设置SelectedItem(以显示当前被选中的Employee)。 从MainList中,我可以获得应该从ChildList中选择的雇员。 显然我不能通过绑定来做到这一点。 不幸的是,我无法在后面的代码中获取SelectedItem属性。 基本上,我需要遍历DataGrid的每一行并获取应在ComboBox中选择的Item。 然后,我将遍历ComboBox项目,直到找到匹配的项目并将其设置为SelectedItem。 但是我找不到办法。
我也尝试使用DataGridComboBoxColumn,但它仅具有SelectedItemBinding属性,并且由于无法在绑定时进行比较,因此这不起作用。 我还尝试使代码中的每个单元格都隐藏在一个ComboBox中,但到目前为止没有成功。
<DataGrid x:Name="DgvProjectTeam" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" Margin="0" RowHeight="40" CanUserAddRows="False" BorderThickness="1" VerticalScrollBarVisibility="Auto" HorizontalGridLinesBrush="#FFA2B5CD" VerticalGridLinesBrush="#FFA2B5CD" ItemsSource="{Binding}" VirtualizingStackPanel.IsVirtualizing="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Resource" Width="200" x:Name="DgtProjectCoreTeam">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="CbxResource" ItemsSource="{Binding Path=ListOfPossibleResources}" DisplayMemberPath="ResourceOfQMatrix.Fullname"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
DataGrid显示了我需要的一切。 我只是不知道如何在后面的代码中为每个生成的ComboBoxCells设置SelectedItem。
有人知道吗?
这是您可以做什么的简单示例。 首先,定义将绑定到DataGrid
视图模型。 理想情况下,这些视图模型在更改其属性时会引发PropertyChanged
或CollectionChanged
,但是对于此简单示例,则不需要这样做。
public class ViewModel
{
public List<ProjectRoleViewModel> ProjectRoles { get; set; }
}
public class ProjectRoleViewModel
{
public string Role { get; set; }
public string Employee { get; set; }
public List<string> OtherEmployees { get; set; }
public string SelectedOtherEmployee { get; set; }
}
我已经将一些虚拟值硬编码为视图模型中的数据:
var viewModel = new ViewModel
{
ProjectRoles = new List<ProjectRoleViewModel>
{
new ProjectRoleViewModel
{
Role = "Designer",
Employee = "John Smith",
OtherEmployees = new List<string> {"Monica Thompson", "Robert Gavin"}
},
new ProjectRoleViewModel
{
Role = "Developer",
Employee = "Tom Barr",
OtherEmployees = new List<string> {"Jason Ross", "James Moore"}
}
}
};
然后需要将此视图模型分配给包含DataGrid
Window
或UserControl
的DataContext
。 这是DataGrid
的XAML:
<DataGrid
ItemsSource="{Binding ProjectRoles}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Role}" />
<DataGridTextColumn Binding="{Binding Employee}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding OtherEmployees}"
SelectedItem="{Binding SelectedOtherEmployee, UpdateSourceTrigger=PropertyChanged}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
用户选择可以完成工作的“其他”员工之后,视图模型的SelectedOtherEmployee
将具有选定的值。 在这种情况下,您不需要任何代码,所有内容都包含在视图模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.