【问题标题】:How to Bind data to DataGridComboBoxColumn in DataGrid using MVVM如何使用 MVVM 将数据绑定到 DataGrid 中的 DataGridComboBoxColumn
【发布时间】:2011-04-03 13:29:25
【问题描述】:

这快把我逼疯了。我有一个 DataGrid,它有一个 DataGridComboBoxColumn,我希望用户能够从中进行选择。这是我的网格的基本轮廓。

<DataGrid ItemsSource="{Binding GoalList}" DockPanel.Dock="Bottom" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{Binding LifeAreaList}" Header="Life Area"/>
<DataGrid.Columns>
</DataGrid>

DataGrid 绑定到Goal 类型的对象集合。每个目标都有一个 LifeArea 类型的属性。每个 LifeArea 都有属性 LifeAreaId 和 Name。

数据上下文包含一个可观察的目标集合:GoalList 和一个生活区域列表:LifeAreaList。我希望用户能够为目标选择不同的生活领域。生命区域的名称也需要是显示的值。

编辑


解决方案是必须将 DataGridComboBoxColumn 的 ItemsSource 设置为静态资源。另一种选择是通过代码设置 ItemsSource。

最后我有:

<DataGridComboBoxColumn x:Name="_lifeAreaComboBoxColumn" SelectedItemBinding="{Binding LifeArea}" DisplayMemberPath="Name" Header="Life Area">

在后面的代码中我设置了 ItemsSource:

_lifeAreaComboBoxColumn.ItemsSource = LifeAreaDAL.GetLifeAreas();

如果有机会,我会将其转换为 StaticResource。

【问题讨论】:

  • 您的 DataGridComboBoxColumn 中不需要 DisplayMemberPath 吗?
  • 我已经尝试过,但下拉菜单中没有显示任何内容。
  • 不是说这样就解决了,但是GridComboBoxColumn怎么知道选择了哪个生活区呢?您还需要将SelectedItem 绑定到某个东西。
  • 您是否检查过是否有任何绑定错误并且 PropertyChanged 被触发

标签: wpf mvvm datagrid datagridcomboboxcolumn


【解决方案1】:

你需要做这样的事情(不要射信使):

<DataGridComboBoxColumn Header="Life Area" SelectedItemBinding="{Binding SelectedLifeArea}">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
            <Setter Property="IsReadOnly" Value="True"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

【讨论】:

  • 天哪,太感谢你了!!!我试图为此寻找解决方案 3 周。为什么 DataGrid 中的组合框如此奇怪?为什么它不像您期望的那样遵循逻辑树?我的意思是 DataGrid 在很长一段时间内都是测试版,直到它被集成到 .NET 4.0 中。他们怎么没修好?我很担心这些东西,我想知道我如何学习这些东西?我需要在哪里阅读才能为这个特定问题找到解决方案?请问你是怎么想出来的? :D
  • 我需要为 ComboBox 样式的 DisplayMemberPath 和 SelectedValuePath 添加设置器。
【解决方案2】:

除了绑定您的 SelectedItem 之外,我猜测您的 SelectedLifeArea 属性不是直接从 LifeAreaList 获得的,因此在比较两个值时,即使名称和 id 匹配,它们也会返回 false。如果两个对象的 Id 匹配,您可能需要覆盖 LifeArea 对象的 .Equals 函数以返回 true

public override bool Equals(object obj)
{
    if (obj is LifeArea)
    {
        return this.Id == (obj as LifeArea).Id;
    }
    return false;
}

【讨论】:

    【解决方案3】:

    Up 也可以使用 DataGridTemplateColumn 并将 ComboBox 放入其中,然后将适当的事件连接到它。

    <DataGridTemplateColumn Header="Alpha">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate DataType="models:MyModelDescription">
                <ComboBox ItemsSource="{Binding AlphaLevels, Mode=OneWay}" SelectedItem="{Binding Alpha, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"></ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-21
      • 2014-01-04
      • 2011-07-27
      • 2017-08-05
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多