【问题标题】:wpf listview selecteditem oneway bindingwpf listview selecteditem单向绑定
【发布时间】:2018-07-07 02:18:46
【问题描述】:

我有一个瘦客户端 UI。此 UI 必须始终反映远程服务器值。此 UI 有一个必须具有 OneWay 绑定的列表视图。单击一个项目不会选择它。双击会触发激活远程服务器上的项目的事件,这将更新视图模型中的值,指示该项目是活动项目。 listview 需要显示这个“活动”状态,所以我将 SelectedItem 绑定到这个 viewmodel 属性 OneWay。这是我使用的 XAML:

<ListView ItemsSource="{Binding Songs}" 
      SelectedItem="{Binding CurrentSong, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
      SelectionMode="Single" 
      IsSynchronizedWithCurrentItem="True"

这个列表视图就像根本没有绑定一样,即单击一个项目会选择它,而当视图模型更新时,所选项目不会更新。

此列表视图确实有一个项目模板,但将其注释掉对这个问题没有影响。

viewmodel 绑定属性已正确更新,并且 UI 的所有其余部分都正常工作,除了此列表视图。

以下是tracing=high时发送到输出窗口的绑定跟踪信息:

启动时:

System.Windows.Data Warning: 67 : BindingExpression (hash=42695785): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=42695785): Found data context element: ListView (hash=36240198) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=42695785): Activate with root item MainWindowViewModel (hash=64688352)
System.Windows.Data Warning: 108 : BindingExpression (hash=42695785):   At level 0 - for MainWindowViewModel.CurrentSong found accessor RuntimePropertyInfo(CurrentSong)
System.Windows.Data Warning: 104 : BindingExpression (hash=42695785): Replace item at level 0 with MainWindowViewModel (hash=64688352), using accessor RuntimePropertyInfo(CurrentSong)
System.Windows.Data Warning: 101 : BindingExpression (hash=42695785): GetValue at level 0 from MainWindowViewModel (hash=64688352) using RuntimePropertyInfo(CurrentSong): Song (hash=17014849)
System.Windows.Data Warning: 80 : BindingExpression (hash=42695785): TransferValue - got raw value Song (hash=17014849)
System.Windows.Data Warning: 89 : BindingExpression (hash=42695785): TransferValue - using final value Song (hash=17014849)

然后当属性改变时:

System.Windows.Data Warning: 95 : BindingExpression (hash=42695785): Got PropertyChanged event from MainWindowViewModel (hash=64688352)
System.Windows.Data Warning: 101 : BindingExpression (hash=42695785): GetValue at level 0 from MainWindowViewModel (hash=64688352) using RuntimePropertyInfo(CurrentSong): Song (hash=40270680)
System.Windows.Data Warning: 80 : BindingExpression (hash=42695785): TransferValue - got raw value Song (hash=40270680)
System.Windows.Data Warning: 89 : BindingExpression (hash=42695785): TransferValue - using final value Song (hash=40270680)

【问题讨论】:

  • 也许我真的不明白你想要什么结果。但是我有一种感觉,您必须改用OneWayFromSource,因为正如您所说,它是在服务器上处理的
  • 谢谢解答,但是没有OneWayFromSource,就是语义上的OneWay,不然就用OneWayToSource。
  • 对不起!把那些东西弄糊涂了!

标签: c# wpf listview


【解决方案1】:

所以这个问题的答案在BindingExpression hash=???中找到了跟踪输出。 Song 对象需要覆盖 Equals 和 GetHashCode 方法,因为默认使用引用相等,并且远程服务器将提供等效对象的新实例。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多