【问题标题】:How to get the SelectedItem property from a ComboBox column in a DataGrid wpf如何从 DataGrid wpf 中的 ComboBox 列中获取 SelectedItem 属性
【发布时间】:2019-07-09 17:49:12
【问题描述】:

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

谁有想法?

【问题讨论】:

  • 您可以通过绑定来做任何事情 - 假设您有一个数据项列表,您应该将这些数据项包装在可以将网格绑定到的视图模型中。轻量级 GridRow 包装器(一般类型的包装器)可用于提供网格可以管理的附加属性,例如 SelectedItem - 然后可以绑定到,然后您可以举例说明绑定的视图模型,而不是编写特定于网格的代码-在后面。
  • 认为我在手机上写了这条评论并且自动更正(auto-mangle?)已经成功了,希望你能明白 :)
  • 感谢 Charleh 的回答。我像你一样尝试过,并且用 ViewModel 提到了 redcurry。这似乎按照我想要的方式工作。非常感谢您的努力!

标签: wpf combobox datagrid code-behind selecteditem


【解决方案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"}
        }
    }
};

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

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

【讨论】:

  • 非常感谢您的“快速示例”:D。周五谷歌很火,但没有人提到要使用视图模型。作为 WPF 开发人员,使用视图模型是否常见?上次我在 WPF 中编程是在学习期间,所以也许我忘记了这一点。但它的工作原理和解决这个问题的方法比我尝试的方法更容易(这也不起作用)。所以再次感谢。非常感谢您的回答!度过了我的星期一!
  • 我很高兴它能正常工作!强烈建议在 WPF 应用程序中使用视图模型(通常是 MVVM)。我还建议使用 MVVM 框架或工具包来帮助您(例如,MVVM Light Toolkit)。
  • 再次感谢!因为我们只是重构了我们的 UserControls 和我们的逻辑的一小部分,所以我必须使用 MVC。希望明年我可以开始一个新项目,而不是使用 MVVM :D
猜你喜欢
  • 2014-05-30
  • 2020-10-27
  • 1970-01-01
  • 2012-12-09
  • 2016-06-15
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 2018-01-23
相关资源
最近更新 更多