【问题标题】:datagrid column does not update数据网格列不更新
【发布时间】:2011-12-14 16:48:00
【问题描述】:

我有一个带有 DataGrid 的 WPF 表单。此 DG 包含一个包含 ComboBox 的 DataGridTemplateColumn。当我单击 DG 的新行并从 Combobox 中选择一个值,然后按 Tab 键转到下一列时,所选值不会在组合框列中保持可见。

但是,当我返回时,组合框中会显示正确的值。

当我关闭列时,如何保持所选值显示在组合框列中?

这是我的组合框列 xaml:

<DataGridTemplateColumn Header="Type" Width="160">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding LocationType.Description, Mode=TwoWay}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <ComboBox Name="cboAddrtype"
                      ItemTemplate="{StaticResource dtAddrType}" IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=1}, Path=DataContext.LocationTypesObject, Mode=OneTime}"
                      SelectedItem="{Binding Path=SelectedLocationType, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
                      SelectedValue="{Binding Path=LocationTypeKey, Mode=TwoWay}"
                      SelectedValuePath="InternalKey"
                      Width="100" />

        </StackPanel>

    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

【问题讨论】:

  • 为什么你同时拥有SelectedItemSelectedValue?它们是设置相同属性的不同方式。我建议去掉SelectedItem,因为它通过引用而不是值来比较项目,并且因为你有一个OneWay 绑定。
  • 更新。我将所选项目更改为 LocationType,现在它可以工作了。就我而言,我需要 SelectedItem 和 SelectedValue 才能正常工作。

标签: wpf mvvm datagrid combobox


【解决方案1】:

CellTemplate 中,您正在绑定LocationType.Description,在CellEditingTemplate 中,您正在绑定SelectedLocationTypeLocationTypeKey。我不确定你从ComboBox 绑定不会以某种方式影响LocationType.Description

此外,SelectedLocationTypeLocationTypeKey 不在 DataTemplate 的上下文中。您应该添加一个 Source 绑定属性来获取您的 ViewModel。

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 2020-12-06
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 2023-02-06
    • 2012-12-24
    相关资源
    最近更新 更多