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

如何从DataGrid wpf中的ComboBox列获取SelectedItem属性,wpf,combobox,datagrid,code-behind,selecteditem,Wpf,Combobox,Datagrid,Code Behind,Selecteditem,最近我们开始在工作中使用WPF。 现在,我想从对象列表DataGrid ItemSource创建一个DataGrid,DataGrid ItemSource包含一个项目角色、应该执行该工作的员工以及也可以执行该工作的员工列表。让我们把这个列表称为MainList。在该DataGrid中,有一个组合框列,它使用另一个对象列表作为ItemSource,您可以在其中更改作业的员工。我将把这个列表称为儿童列表。这个列表包括在allready提到的MainList中,我使用正确的BindingPath绑定

最近我们开始在工作中使用WPF。 现在,我想从对象列表DataGrid ItemSource创建一个DataGrid,DataGrid ItemSource包含一个项目角色、应该执行该工作的员工以及也可以执行该工作的员工列表。让我们把这个列表称为MainList。在该DataGrid中,有一个组合框列,它使用另一个对象列表作为ItemSource,您可以在其中更改作业的员工。我将把这个列表称为儿童列表。这个列表包括在allready提到的MainList中,我使用正确的BindingPath绑定它。到现在为止,一直都还不错。现在我必须设置SelectedItem以显示当前选择的员工。从Main列表中,我可以获得应从ChildList中选择的员工。显然,我不能通过装订来做到这一点。不幸的是,我无法在代码隐藏中获得SelectedItem属性。基本上,我需要遍历DataGrid中的每一行,并获取应该在组合框中选择的项。然后我将遍历组合框项目,直到找到匹配的项目并将其设置为SelectedItem。但是我找不到一个方法来做这件事

我也尝试过使用DataGridComboxColumn,但它只有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的视图模型。理想情况下,这些视图模型在其属性更改时会引发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; }
}

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

您可以使用绑定完成所有操作-假设您有一个数据项列表,您应该将这些数据项包装到一个视图模型中,您可以将网格绑定到该视图模型。一个轻量级GridRow包装器一个通用类型的包装器可以用来提供网格可以管理的其他属性,比如SelectedItem,然后可以绑定到它,然后你可以对绑定的viewmodel进行示例,而不是编写特定于网格的代码。你认为我在手机上写了这个评论并自动更正了auto-mangle吗?已经有了它的方式,希望你得到的想法:谢谢你的回答查理。我试过了,就像你和redcurry提到的ViewModel一样。这似乎是我想要的工作方式。非常感谢你的努力!非常感谢您的快速示例:D.谷歌在周五非常火爆,但没有人提到使用viewmodel。作为WPF开发人员,使用viewmodels是否常见?我上一次在WPF编程是在学习期间,所以我可能忘记了这一点。但是它是有效的,而且它是解决这个问题的一种比我尝试的方法简单得多的方法,这些方法也不起作用。再次感谢你。非常感谢您的回答!我的星期一成功了!我很高兴它起作用了!强烈建议在WPF应用程序中使用视图模型和一般视图。我还建议使用MVVM框架或工具包来帮助您,例如,再次感谢!因为我们只是重构我们的用户控件,而我们的逻辑只有一小部分,所以我必须使用MVC。希望我能在接下来的几年里开始一个新的项目,然后我会使用MVVM:D
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
    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>