Click here to Skip to main content
12,405,761 members (68,259 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

28.7K views
3 bookmarked
Posted

Binding to the selected items of a ListBox (or another items controls)

, 14 Jun 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
A problem you often meet when using MVVM is to get the selected items of an items control, especially Listbox.

A problem you often meet when using MVVM is to get the selected items of an items control, especially Listbox.

You can easily bind the selected item or the current items but when multi selection comes in the way, it becomes harder because the SelectedItems (with an 's' property is not available to binding).

In this article, we will discover an easy way to bind yourself with an attached property to the SelectedItems property of the ListBox control.

We will use the Ramora pattern discussed before to bind ourself to the ListBox's selectionChanged events and then update the target list when the selection change.

If the target list implements INotifyCollectionChanged, we will update the listbox selection when this event is raised. Here is the resulting code:

public class ListBoxSelectedItemsSyncher : DependencyObject
{
  private static List<Syncher> _synchers = new List<Syncher>();
 
  #region ListToSync
 
  /// <span class="code-SummaryComment"><summary>
</span>

Here we are.
Edit: I found out later another version of the same feature made by Samuel Jack on his blog: http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html !

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

jmix90
Software Developer http://wpf-france.fr
France (Metropolitan) France (Metropolitan)
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.


You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :

You may also be interested in...

Comments and Discussions

 
QuestionWhat's with all the URL's in your code? Pin
tonyt14-Jun-10 8:37
membertonyt14-Jun-10 8:37 
AnswerRe: What's with all the URL's in your code? Pin
jmix9014-Jun-10 11:55
memberjmix9014-Jun-10 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 Jun 2010
Article Copyright 2010 by jmix90
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid