wpf listview selecteditem oneway binding

I have a thin client UI. This UI must always reflect the remote server values. This UI has a listview that must have a OneWay binding. Clicking on an item does NOT select it. Double-clicking triggers an event that activates the item on the remote server, which will update a value in the viewmodel indicating that this item is the active item. The listview needs to display this "active" state, so I bind the SelectedItem to this viewmodel property OneWay. Here is the XAML that I use:

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

This listview acts as if there is no binding at all, ie clicking an item selects it and the selected item is not updated when the viewmodel is updated.

This listview does have an itemtemplate but commenting it out has not effect on this problem.

The viewmodel binding property is updated correctly and all the rest of the UI is working correctly except for this listview.

The following is the binding trace info sent to the output window when tracing=high:

At startup:

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)

and then when the property changes:

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)

So the answer to this question was found in the BindingExpression hash=??? trace output. The Song object needs to override the Equals and GetHashCode methods because the default uses a reference equality and the remote server will server up a new instance of the equivalent object.

链接地址: http://www.djcxy.com/p/44688.html

上一篇: 如何通过peerJS点对点连接接收数据?

下一篇: wpf listview selecteditem单向绑定