2015-04-01 352 views
0

我成功地将一个集合绑定到DataGrid,并成功将一个属性绑定到DataGridComboBoxColumn。 (有一个名为snoop的WPF工具,允许我调查数据是否已被绑定)。WPF DataGridComboBoxColumn不显示初始绑定值

但由于某些原因未显示初始数据。只有在手动更改选择后。该值显然可用。

任何提示或帮助表示赞赏!

谢谢

这是我的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> 
+0

硬,没有看到你的对象说了。我假设你正确使用INotifyPropertyChanged? – 2015-04-01 13:21:21

+0

是的,肯定。我确定这不是一个绑定问题,因为我可以通过snoop或WPF检查器查看绑定值。在我看来,它一定是XAML中的一些东西 – user1841243 2015-04-01 13:29:29

回答

1

覆盖equals方法,因为很可能你的产品实际上并不相同的实际情况作为您的项目集合中的一个,所以绑定不起作用。 Snoop将显示相同的值,所以您可能会认为它是相同的,但实际上并不相同。把这个在你的类中定义的对象,更换MyClasswith类类型等

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?

+0

我给你一个大拇指,让我在正确的方向上放置,因为绑定对象和列表中的对象之间存在不匹配。我已经有Equals方法,所以这对我没有帮助。我仍然有问题解决这个问题......目前我有一个肮脏的解决方法,我将向下移动可视化树并填充网格加载后的文本块(通过代码后面)。不是很好的解决方案... – user1841243 2015-04-02 11:37:11

相关问题