wpf listview selectitem oneway binding

时间:2018-01-28 05:22:19

标签: c# wpf listview

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

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

此列表视图就像没有任何绑定一样,即单击某个项目会选择它,并且在更新视图模型时不会更新所选项目。

这个listview确实有一个itemtemplate但是注释掉它对这个问题没有影响。

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

以下是跟踪= 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)

1 个答案:

答案 0 :(得分:-1)

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