Today one of my colleagues at Infusion asked me how he could sync up the selection of a ListBox with his ViewModel. WPF supports already single mode selection via the ICollectionView yet when it comes to MultiSelect there is no out of the box support in WPF.
Attached Properties to the rescue …..🙂
I quickly when in my VS and wrote up a simple Attached property that hooks to the SelectionChanged event of the ListBox and populates a list of selected items to the ViewModel.
This is how the attached property looks like
and here is how you would use it in the XAML
As you can see all you have to set is the List you want to populate in the ViewModel and the rest is taken care of by the Attached Property.
Please Note: This code was implemented quickly without any testing to prove an implementation idea, so please do review it before putting it in production🙂