【问题标题】:WPF DataGridComboBoxColumn not showing initial binding valueWPF DataGridComboBoxColumn 未显示初始绑定值
【发布时间】:2015-04-01 13:12:51
【问题描述】:

我成功地将集合绑定到 DataGrid,并且还成功地将属性绑定到 DataGridComboBoxColumn。 (有一个名为 snoop 的 WPF 工具可以让我调查数据是否已绑定)。

但由于某种原因,没有显示初始数据。只有在我手动更改选择之后。该值是可见的。

感谢任何提示或帮助!

谢谢,

这是我的 XAML:

                            <DataGridComboBoxColumn Width="*"
                                                    DisplayMemberPath="RedOms"
                                                    Header="MyHeader"
                                                    ItemsSource="{Binding Source={StaticResource MyModel},
                                                                          Path=SRCollection,
                                                                          Mode=OneWay}"
                                                    SelectedValueBinding="{Binding AZSR,
                                                                                   Mode=TwoWay}"
                                                    SelectedValuePath="ID">
                                <DataGridComboBoxColumn.CellStyle>
                                    <Style BasedOn="{StaticResource EDGridCell}" TargetType="DataGridCell">
                                        <Setter Property="IsEnabled" Value="False" />
                                        <Style.Triggers>

                                            <DataTrigger Binding="{Binding AZBev, Mode=OneWay}" Value="False">
                                                <Setter Property="Background" Value="{StaticResource KlrColor}" />
                                                <Setter Property="IsEnabled" Value="True" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGridComboBoxColumn.CellStyle>

                                <DataGridComboBoxColumn.EditingElementStyle>
                                    <Style TargetType="ComboBox">
                                        <Setter Property="Background" Value="{StaticResource KlrColor}" />
                                    </Style>
                                </DataGridComboBoxColumn.EditingElementStyle>
                            </DataGridComboBoxColumn>

这里是静态资源 EDGridCell

    <Style x:Key="EDGridCell" TargetType="{x:Type DataGridCell}">
        <EventSetter Event="UIElement.PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Yellow" />
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>

【问题讨论】:

  • 很难说没有看到你的对象。我假设您正确使用 INotifyPropertyChanged?
  • 是的,当然。我确定这不是绑定问题,因为我可以通过 snoop 或 WPF 检查器查看绑定值。在我看来,它一定是 XAML 中的东西

标签: c# wpf xaml


【解决方案1】:

覆盖 Equals,因为您的项目实际上可能与项目集合中的实际实例不同,因此绑定不起作用。 Snoop 将显示相同的值,因此您可能认为它相同,但实际上并非如此。将其放入定义对象的类中,将 MyClass 替换为您的类类型等。

    public override bool Equals(object obj) 
{ 
    if (obj == null || !(obj is MyClass)) 
        return false; 

    return ((MyClass)obj).Id == this.Id); 
}

更多信息: https://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/#comments

Why is WPF ComboBox item not updating?

【讨论】:

  • 在绑定对象和列表中的对象之间存在不匹配的意义上,我要向您竖起大拇指,让我朝着正确的方向前进。我已经有了 Equals 方法,所以这对我没有帮助。我仍然无法解决这个问题......目前我有一个肮脏的解决方法,我在加载网格后向下移动可视树并填充文本块(通过后面的代码)。不是一个很好的解决方案...
猜你喜欢
  • 2013-05-31
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2017-06-25
  • 2011-07-27
  • 1970-01-01
  • 2017-04-02
相关资源
最近更新 更多