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

问题描述 投票:0回答:1

最近我们开始在工作中使用WPF。现在,我想从包含项目角色的对象列表(DataGrid ItemSource),应该完成工作的员工以及也可以完成该工作的员工列表中创建DataGrid。让我们将此列表称为“MainList”。在该DataGrid中是一个ComboBox列,它使用另一个对象列表作为ItemSource,您可以在其中更改作业的员工。我将此列表称为“ChildList”。这个列表包含在MainList中(如前所述),我使用正确的BindingPath绑定它。到现在为止还挺好。现在我必须设置SelectedItem(以显示当前选择的Employee)。从MainList我可以获得应该从ChildList中选择的员工。显然我不能通过Binding来做到这一点。不幸的是,我无法在代码隐藏中获得SelectedItem属性。基本上我需要遍历DataGrid中的每一行并获取应该在ComboBox中选择的Item。然后我将浏览ComboBox项目,直到找到匹配的Item并将其设置为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显示了我需要的一切。我只是不知道如何为代码隐藏中的每个生成的ComboBox单元设置Selected Item。

有人有想法吗?

wpf combobox datagrid code-behind selecteditem
1个回答
1
投票

以下是您可以做的快速示例。首先,定义将绑定到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"}
        }
    }
};

然后需要将此视图模型分配给包含DataContextWindowUserControlDataGrid。这是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将具有所选值。在这种情况下,您不需要任何代码隐藏,视图模型中包含所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.