Possible to bind collection items to checked items of a checked listbox in WPF?

kayhustle
kayhustle used Ask the Experts™
on
If I have a checked listbox in wpf, is it possible to bind a collection's items to the actual checked items of the listbox?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What exactly do you want the checkbox to represent? Is it a property on the bound object or is it related to the IsSelected property of the list itself?

Author

Commented:
I want the checkbox to hold a list of items, yet the selected items of the checkbox be bound to a property on the control called SelectedItems, which is a collection of all selected items.
I oftentime bind the SelectedItem to a property in the view model, but I've never tried maintaining a mult-selection list.

A quick search turned up this link, which looks like it should help.

http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
That focuses on selected items.  Is there any way to get the collection of checked items in the ListBox though?
Well, that's why I asked about what you wanted ... selected or checked.

If you aren't interested in selected items, then you don't want to use a checked listbox. You want to simply have a one of the columns contain a checkbox, which is bound to a property on the object. At that point, when the user checks/unchecks, you can easily add/remove the item from a collection in the property setter (prob through an event). Or, you can postpone the building of the collection to when you actually need it and just enumerate the collection.

Author

Commented:
Right, yeah see I didn't know if you could automatically bind the checked items to an ObservableCollection lets say and vice versa.  So when I want to load the control, the checked items are automatically bound to the items in the ObservableCollection.
If the checkbox is bound to a property on the domain object, then changes to either end (two-way) affect either end on a one-to-one basis. No collection involved.

If you were dealing with Selected Items, on the other hand, it seems that it can be done.

Author

Commented:
Ok, so I'm assuming this isn't directly supported in WPF.  I'll accept your solution.  Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial