Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6552951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:31:21+00:00 2026-05-25T12:31:21+00:00

i have a ListBox with its ItemsSource bound to a CollectionViewSource, which is bound

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:31:22+00:00Added an answer on May 25, 2026 at 12:31 pm

    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.

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    

    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.

    <DataTemplate DataType="{x:Type local:MyClassViewModel}">
        <Border BorderBrush="#FF036200" BorderThickness="1" Background="#FF3CC600" CornerRadius="10">
            <Grid Height="Auto" Margin="4" DataContext={Binding ModelClass}>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*"/>
                    <ColumnDefinition Width="0.5*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.5*"/>
                    <RowDefinition Height="0.5*"/>
                </Grid.RowDefinitions>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding StringProp1}" VerticalAlignment="Top" Margin="0" FontSize="16"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding StringProp2}" VerticalAlignment="Top" Margin="0" Grid.Row="1" FontSize="16"/>
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding StringProp3}" VerticalAlignment="Top" Grid.Column="1" Margin="0" FontSize="16"/>
                <CheckBox Content="Is Bool True?" HorizontalAlignment="Left" IsChecked="{Binding BoolProp}" VerticalAlignment="Top" Grid.Column="1" Margin="0" Grid.Row="1" FontSize="16"/>
            </Grid>
        </Border>
    </DataTemplate>
    

    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.

    <DataTemplate DataType="{x:Type local:MyClassViewModel}">
            <Border BorderBrush="#FF036200" BorderThickness="1" Background="{Binding BorderBackground}" CornerRadius="10">
    

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF Listbox defined in a DataTemplate that has its ItemsSource bound
I have a listbox which is bound to a CollectionViewSource (backed by an ObservableCollection).
I have a WPF ListBox with its ItemsSource bound to an ObservableCollection navigation property
[Original] I have a ListBox which has its ItemsSource (this is done in the
I currently have a listbox that has its selected item bound to a property
I have a listbox that has its ItemsSource set to a Dictionary(Of String, ColumnMetadata)
I am new to WPF. I have a ListBox that has its ItemSource set
I have Window that has a ListBox ListBox(MyListBox) has a DataTable for its DataContext
I have a listbox and I want its ItemsSource to be all the items
I have a WPF ListBox control and I'm setting its ItemsSource to a collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.