如何从 DataGrid wpf 中的 ComboBox 列获取 SelectedItem 属性

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 中选择的项目。然后我将遍历 ComboBox 项目,直到找到匹配的项目并将其设置为 SelectedItem。但是我找不到办法做到这一点。

我也尝试使用 DataGridComboBoxColumn,但它只有 SelectedItemBinding 属性 并且由于您无法在绑定时进行比较,所以这应该不起作用。 我还尝试获取代码隐藏中的每个单元格,这是一个组合框,但到目前为止没有成功。

<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 显示了我需要的一切。我只是不知道如何在代码隐藏中为每个生成的 ComboBoxCell 设置 SelectedItem。

有人有想法吗?

这里是您可以执行的操作的简单示例。首先,定义将绑定到 DataGrid 的视图模型。理想情况下,这些视图模型会在属性更改时引发 PropertyChangedCollectionChanged,但对于这个简单的示例,不需要这样做。

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"}
        }
    }
};

然后需要将此视图模型分配给包含 DataGridWindowUserControlDataContext。这是 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>

用户选择 "other" 可以胜任该工作的员工后,视图模型的 SelectedOtherEmployee 将具有所选的值。在这种情况下,您不需要任何代码隐藏,所有内容都包含在视图模型中。