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.
What are the checkboxes bound to? 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
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.
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
And in the ViewModels that inherit: