tags:

views:

154

answers:

1

I have a listview control that is bound to an observable collection that I frequently update. When I update the collection, I clear the collection and then add the values. While this is a bit overkill for simple changes, it's very simple and accomplishes the job. In doing so, I lose which item was selected in my listview control and cannot figure out how to maintain the selected state after an update.

I initially thought I could two-way bind the SelectedValue by using the following (WPF):

SelectedValue="{Binding Path=SelectedDevice, Mode=TwoWay}

And then update my collection by:

string PreviouslySelectedDevice = this.SelectedDevice;
aCollection.Clear();
// ... Add Items ...
this.SelectedDevice = PreviouslySelectedDevice;

I was hoping the TwoWay binding would allow me to make changes to backend property which would be automagically reflected in the listView control but this doesn't work. Thanks in advance!

A: 

It seems like your binding should be updating SelectedValue. Is SelectedDevice a DependencyProperty or are you implementing INotifyPropertyChanged?

Robert Macnee
SelectedDevice is a KeyValuePair<int,string> that stores the selected device (SelectedDevice ID, SelectedDevice Name). I suspect the problem is that since KeyValuePair doesn't implement INotifyPropertyChanged, the control isn't being updated as I intended.
Joel
It would be the class which has the SelectedDevice property that would need to implement INotifyPropertyChanged, or the SelectedDevice property would need to be a DependencyProperty.
Robert Macnee