ListViewExtensions Select From ViewModel

Mar 28, 2013 at 5:34 PM
I am looking for a way to select multiple items in a ListView from the view model. This extension appears to help most of the way.

I have a SelectedItems property in my view model which is an ObservableCollection<Item> to which the attached property BindableSelection is bound. When I need certain items to appear selected in the view I am simply adding them to this SelectedItems property.

Initially it all looked good. However, I noticed an odd behavior. If I add 10 selected items and then inspect the SelectedItems collection it will have 20 items. It seems that when I add an item in the vm that causes the ListView to add the item to its selected list which in turns causes the item to be added a second time to my SelectedItems collection.

I opened up the sample code for this and simply added an IsUpdating flag and use it to short circuit updating items in either the boundSelection or the ListView.SelectedItems if the other is in the process of updating.

Hopefully this makes sense. So 2 questions. First, am I going about this the right way or am I headed in the wrong direction? Second, if this is the right approach how might I go about correcting this issue?
Mar 28, 2013 at 5:54 PM
Just to confirm - are you using the latest source or a NuGet package? There have been some updates in the source recently that haven't made it to NuGet.
Mar 28, 2013 at 9:40 PM
I did get it via NuGet. The version I have is reported in Nuget as
Mar 28, 2013 at 9:42 PM
Sorry, I have been really busy recently and never published the current source packaged for NuGet. You should try taking the source from CodePlex. Just that single file with ListViewExtensions should work and check if that works. If it does not work - you can always try to fix it yourself.
Mar 29, 2013 at 6:01 AM
I linked in all of the relevant files and it does appear to work. But I must admit that I cannot see why it works. If you have a moment perhaps you could enlighten me. I'm one of those who likes to understand the code I'm working with a bit deeper.

Thanks for your work and assistance.
Apr 8, 2013 at 4:05 PM
In the ListViewBindableSelectionHandler - Attach method the bound selected items collection needed to be added to the list box selected items.