OriTester1 on Thu, 12 Jan 2012 10:49:31
I have a multiSelect ListBox and I am trying to set the IsSelected property of the list items to be 'true' in XAML, so that the items appear selected as they are added automatically. However, as items are added, they are not shown as selected. Conversely, I can set the HorizontalContentAlignment to 'Right' and this does work - they appear right-aligned as expected.
This simple test app reproduces the problem:
1. Why does the HorizontalContentAlignment value work while the IsSelected property value is ignored?
2. How can I add selected items WITHOUT explicitly creating ListBoxItem objects (my actual application binds the list items to a custom collection in my ViewModel).
Benjamin Roux [MSFT] on Thu, 12 Jan 2012 21:25:28
IsSelected is for single selection.
OriTester1 on Fri, 13 Jan 2012 10:30:03
Thanks for the reply. In code, you can indeed add items to the SelectedItems property:
And this works as expected, but my actual application is using MVVM and I would prefer to use bindings if possible. The SelectedItems property is not accessible via XAML, so I'm thinking the only way to do this will be to write code-behind (not ideal, but doable)?
Benjamin Roux [MSFT] on Fri, 13 Jan 2012 15:25:07
Since SelectedItems is in read-only (so you can't bind anything to it), the workaround is to create a behavior with a Dependency Properties named SelectedItems (you will bind to this property). The behavior will do the bridge between your ViewModel and the SelectedItems of the ListBox.
OriTester1 on Tue, 17 Jan 2012 17:12:58
Thank you for the suggestion. I'm new to Windows Phone 7 and I'm not sure what you mean exactly.
Could you please elaborate in more detail where the dependency property 'SelectedItems' should go? In my custom Behaviour class? What would the XAML then look like?
OriTester1 on Wed, 18 Jan 2012 15:42:05
Success. I managed to get this working by creating a 'Behaviour' class called ListBoxMultipleBehaviour containing a single custom SelectedItems Dependency Property. This class can then be referenced in XAML to attach this custom property to any ListBox control. So the ListBox control is given an extra property called ListBoxMultipleBehaviour.SelectedItems and most importantly, this custom property can be bound to the ViewModel, the same way any other property can.
So I bind the ListBoxMultipleBehaviour.SelectedItems property in XAML to my ViewModel's SelectedItems property and I'm then able to do the necessary workaround within the DependencyPropertyChanged callback to physically add the elements to the ListBox's ACTUAL SelectedItems.
Source code below in case anyone is interested.
This causes all the items to be selected automatically. Alternatively, the SelectedItems property in the ViewModel can be modified to return only the items you wish to be selected.
Just change 'TestProject' to whichever namespace your project has.
Thanks to Benjamin for setting me on the right path and to this guy for seeing me through:
Sid_ucer on Mon, 03 Dec 2012 11:38:33
Just what I was looking for.
Works like a charm with a few modifications in case you wanted to bind n items as selected items.
Thanks a lot!
Lightray_DVD on Wed, 24 Apr 2013 15:31:38
I tried your solution on VS2010-WP7.1.
I defined a second list into viewmodel in order to have the SelectedItems list but the "OnSelectedItemsPropertyChanged" method is called at page loading only.
I think that the problem is into DependencyProperty definition because I tried a solution with AttachedCommandBehavior with the same result.
Could you help me?