How to find the right item out of a Listbox bound to a CollectionViewSource?

If it is bound to a property on the objects in your collection, then you shouldn't need to figure out which Checkbox was clicked. If it is not bound to something on the object or ViewModel you might be able to get the SelectedItem from the listbox.

Up vote 1 down vote favorite share g+ share fb share tw.

I have a ListBox with its ItemsSource bound to a CollectionViewSource, which is bound to an ObservableCollection. The template for the ListBoxItem includes a CheckBox which, when checked, indicates that the item is selected. My Problem is, that I have no idea how to find out which items have their CheckBox clicked.

C# wpf mvvm listbox link|improve this question edited Sep 9 '11 at 17:16Will?50.6k1494177 asked Sep 8 '11 at 13:58StefanG326.

This could help: stackoverflow.com/questions/1271375/… – Davide Piras Sep 8 '11 at 14:08 that looked good but it doesn't seem to work for me. I added this code into my code behind file : EventManager. RegisterClassHandler(typeof(SurfaceListBoxItem), SurfaceListBoxItem.

MouseLeftButtonDownEvent, new RoutedEventHandler(this. OnMouseLeftButtonDown)); private void OnMouseLeftButtonDown(object sender, RoutedEventArgs e) { MessageBox. Show("Mouse Down"); } but the event never gets fired :( – StefanG Sep 8 '11 at 14:29 I found a workaround: in the ItemContainerStyle of the Listbox I added the sender object of this event is the ListboxItem I clicked on.

Now I can get the source object from the listboxitem. Content property, so I know which Item in the dataset I have to change. In my oppinion this is a horrible solution but it works for now.

If anyone knows, how to do it right, please let me know – StefanG Sep 8 '11 at 14:57.

If it is bound to a property on the objects in your collection, then you shouldn't need to figure out which Checkbox was clicked. If it is not bound to something on the object or ViewModel you might be able to get the SelectedItem from the listbox. Previously I have bound the SelectedItem property of the listBox to a property on my ViewModel so that I can have things that run whenever it changes.

As to getting the index, you should be able to match the idex returned from the listbox with an index of an item in the CollectionViewSource. View which contains the current view of the collection in the order it is displayed. If you are not using MVVM, I would suggest it.

I started out not using it and quickly got mired in the code-behind. Example in MVVM Lets say we have MyClass with three string properties and a boolean. In MVVM, we have a MyClassViewModel which has a property to contain an instance of MyClass along with any needed functionality for the View (a listboxitem in this case).

We also have a MyWindowViewModel which will hold the collection of data, and other stuff for our main view. Example ViewModels Public Class MainViewModel Inherits ViewModelBase Public Property MyClassCollection as New ObservableCollection(Of MyClassViewModel) End Class Public Class MyClassViewModel Inherits ViewModelBase Public Property ModelClass as MyClass Public Sub New() End Sub Public Sub New(ByRef CustClass as MyClass) ModelClass = CustClass End Sub End Class When we get our data, we put it in the ObservableCollection(Of MyClassViewModel). I usually do this in the WorkCompleted handler for a data retrieval backgroundworker.

For Each mc as MyClass in e. Results MyClassCollection. Add(New MyClassViewModel(mc) Next The listbox will still get it's items from the observable collection through the collectionViewSource, but now they will be of type MyClassViewModel.

Thus when someone clicks the checkbox, it changes the value on the object underneath, and since the listbox item represents the ViewModel, if you want to change something on the ListBoxItem, databind it to a property on the ViewModel and change that property. For instance, lets say you want to change the color of the ListBoxItem to a random color when a user checks the checkbox (and for whatever reason you don't want to use a trigger or something. You could create a property on the MyClassViewModel of type Brush and Databind the Border.

Background to it, and a property of Boolean which sets the MyClass property to the same value. In the setter for the boolean property, you check the value, and if it is true, set the brush value (Random Brush generator not included). This way the ViewModel tells the view how to display the data in the Model, and can intercept datachanges from the View and do something with it if necessary.

Reply to Comment Everyone kind of has their own way of doing MVVM. I have several different kinds of ViewModels that I use. Some are really Form View Models (Used to control the way a form works), Model View Models (used to tell the View generally a usercontrol for editing details or a ItemsControl DataTemplate how to display the data).

With Form View Models, I sometimes break them down to NavigationViewModel and Record Maintenance ViewModels depending on the situation. In this case I really have a ViewModel for controlling the form and a Viewmodel for displaying the data. The Form View Model often handles button commands for adding or removing items in a collection, or specifying logic that tells the View whether the save or other action button is enabled.

INotifyPropertyChanged Very light ViewModelBase class implementing INPC Imports System. ComponentModel Public MustInherit Class ViewModelBase Implements INotifyPropertyChanged Public Event PropertyChanged As PropertyChangedEventHandler _ Implements INotifyPropertyChanged. PropertyChanged Protected Sub OnPropertyChanged(ByVal info As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info)) End Sub End Class And in the ViewModels that inherit: Public Property IsSelected() As Boolean Get Return m_IsSelected End Get Set(ByVal value As Boolean) m_IsSelected = value OnPropertyChanged("IsSelected") End Set End Property.

Thanks for your post. As I mentioned I unfortunately can't use the SelectedItem property of the listbox because a click on a checkbox doesn't automatically select the parent ListboxItem. I now bound the Checkboxes Tag Property to a string Value in my Datasourceobject.

With it I can figure out, which item has changed. Would there be a more elegant way with mvvm? – StefanG Sep 8 '11 at 15:34 See my post for some examples.

If you have some specific functionality in mind, let me know, and I can give you an example of how I would do it in MVVM pattern. – CodeWarrior Sep 8 '11 at 16:35 that's what I call a nice answer. Thank you.

I mark it as the answer because it allready helped me. The only thing that I wonder is, why do I have to create a ClassViewModel? Wouldn't it also be in the sense of mvvm if I put my modelClass inside the Observablecollection?

– StefanG Sep 9 '11 at 11:39 Explanation was a little verbose, so I put it in the post above... – CodeWarrior Sep 9 '11 at 15:34 Also keep in mind that for this to work properly, you need to implement INotifyPropertyChanged on any classes you want to notify the UI that a property has changed value, as well as on the individual property. Example will be above... – CodeWarrior Sep 9 '11 at 19:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions