【问题标题】:WPF combobox bindingWPF 组合框绑定
【发布时间】:2011-03-09 03:33:52
【问题描述】:

更新:

我在 SelectedValue 属性 (diagnostics:PresentationTraceSources.TraceLevel=High) 中包含了一些诊断信息,我想我可以看到这个问题,尽管我不知道如何解决它。诊断显示

System.Windows.Data Warning: 76 : BindingExpression (hash=16001149): TransferValue - got raw value '3'
    System.Windows.Data Warning: 80 : BindingExpression (hash=16001149): TransferValue - implicit converter produced '3'
    System.Windows.Data Warning: 85 : BindingExpression (hash=16001149): TransferValue - using final value '3'
    System.Windows.Data Warning: 86 : BindingExpression (hash=16001149): **Update - got raw value <null>**
    System.Windows.Data Warning: 90 : BindingExpression (hash=16001149): **Update - using final value <null>**
    System.Windows.Data Warning: 98 : BindingExpression (hash=16001149): SetValue at level 1 to LookupModelBase (hash=57292143) using ReflectPropertyDescriptor(PK): <null>
    System.Windows.Data Warning: 91 : BindingExpression (hash=16001149): Got ValueChanged event from LookupModelBase (hash=57292143)
    System.Windows.Data Warning: 85 : BindingExpression (hash=40869743): TransferValue - using final value ObservableCollection`1 (hash=64658589 Count=4)

所以 SelectedValue 出于某种原因设置为 null。

有人解决过这个问题吗?

谢谢

嗨,

进一步my previous post,为了比我之前的胡言乱语更清楚一点,我认为问题如下;

为什么在修改另一个属性时(由于 OnPropertyChanged)组合框显示设置为空白,即使实际基础值没有修改?如果我修改同一记录中的字段,组合框显示为空,但如果我离开并再次返回,值会返回吗?

xaml如下;

            ItemsSource="{Binding TrialParentValues, Mode=OneWay}"
            SelectedItem="{Binding TrialParentObj, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, Mode=TwoWay}"                             
            DisplayMemberPath="Description"             
            SelectedValuePath="PK"
            SelectedValue="{Binding TrialParentObj.PK}"  

谢谢

【问题讨论】:

  • 我已经添加了你之前帖子的链接,你能检查它是正确的吗?

标签: wpf binding combobox


【解决方案1】:

结合使用SelectedValue 上的绑定与SelectedValuePath 或SelectedItem 上的绑定

不要在您的组合框上同时使用两者!

在您的情况下,我建议您删除 SelectedItem 绑定。

【讨论】:

  • 我最初尝试过,但根本无法更新绑定。我遵循了这个建议stackoverflow.com/questions/247413/… 并取得了更大的成功,但仍然存在上述问题。
  • 当您的 ItemsSource 更改时,选定的值绑定会中断。那是个讨厌的问题。请尽量将您的 ItemsSource 作为资源或确保它不是动态的。如果它是动态的,请考虑使用 CollectionViewSource。
  • 好的,谢谢指点。真正让我感到困惑的是,我已经设法让它成功地适用于其他属性,其中更改 ItemsSource 属性不会破坏绑定。然而,尽管搜索了几个小时,我似乎无法弄清楚为什么它在我概述的情况下不起作用。
  • 它是 SelectedValue/Item 和 ItemsSource 的组合。我们已经在我们的项目中多次看到它。它是我所知道的唯一组合。刷新 ItemsSource 会导致破坏 SelectedValue 绑定。
猜你喜欢
  • 2012-06-18
  • 1970-01-01
  • 2015-09-15
  • 2018-01-17
  • 1970-01-01
  • 2012-08-20
相关资源
最近更新 更多