---
title: "How to get Selected items in WPF CheckBox ListBox"  
description: "How to get Selected items in WPF CheckBox ListBox"  
author: "Anonymous User"  
published: 2013-07-18  
updated: 2013-07-18  
canonical: https://www.mindstick.com/forum/1307/how-to-get-selected-items-in-wpf-checkbox-listbox  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# How to get Selected items in WPF CheckBox ListBox

HI [developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs)!

Am Using the [checkbox](https://www.mindstick.com/articles/291/how-should-use-checkbox-control-in-vb-dot-net) in [listbox](https://www.mindstick.com/articles/626/listbox-events-in-c-dot-net) items, how to get the selected checkboxes from the list

<ListBox ItemsSource="{[Binding](https://www.mindstick.com/blog/146/dynamic-binding-in-c-sharp) NameList}" HorizontalAlignment="Left" Margin="16,68,0,12" Name="listBox1" Width="156" IsEnabled="True" SelectionMode="[Multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors)" Focusable="True" IsHitTestVisible="True" IsTextSearchEnabled="False" FontSize="12" Padding="5" SelectionChanged="listBox1_SelectionChanged">

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel [Orientation](https://www.mindstick.com/interview/2604/what-is-orientation)="Horizontal">

<CheckBox [Content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand)="{Binding Path=CNames}" />

</StackPanel>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>

I tried to loop thru the [selected items](https://www.mindstick.com/forum/352/i-need-to-display-my-checkboxlist-selected-items-in-a-label-how-to-do) in the listboxitems, but it throws [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) in listboxitem

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) void btnSelected(object sender, RoutedEventArgs e)

{

foreach (ListBoxItem item in listBox1.Items)

{

if (item.ToString() == "true")

{

MessageBox.Show(item.Content.ToString());

}

}

}

thanks

## Replies

### Reply by shreesh chandra shukla

Solution!

You could move the data context for each of these items away from the UI and create an ObservableCollection of objects

```
public ObservableCollection<CheckedItem> List {
get;set;}public class CheckedItem : INotifyPropertyChanged{  private bool selected;  private string description;  public bool Selected
  {      get { return selected; }     set      {        selected =value;       
OnPropertyChanged("Selected");     }  }  public string Description   {      get { return description; }     set     {         description =value;        
OnPropertyChanged("Description");     }   }  /*
INotifyPropertyChanged implementation */}
```

Then in your ListBox ItemTemplate

```
<ItemTemplate>  <DataTemplate>    <CheckBox
IsChecked="{Binding Path=Selected}"              
Content={Binding Path=Description}" /> 
</DataTemplate></ItemTemplate>
```

Your selected items are now available in the ObservableCollection rather than looping through UI elements


---

Original Source: https://www.mindstick.com/forum/1307/how-to-get-selected-items-in-wpf-checkbox-listbox

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
