Here is an example how to accomplish this.
MainWindow.xaml
<Window x:Class="CheckBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CheckBoxTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ListBox Name="ListBoxOptions">
<CheckBox Content="TEST A"></CheckBox>
<CheckBox Content="TEST B"></CheckBox>
<CheckBox Content="TEST C"></CheckBox>
<CheckBox Content="TEST D"></CheckBox>
</ListBox>
<Button Grid.Row="1"
Width="100"
Height="25"
Margin="5"
Name="ButtonShowResults"
Content="Show Results"
Click="ButtonShowResults_OnClick"></Button>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace CheckBoxTest
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonShowResults_OnClick(object sender, RoutedEventArgs e)
{
var selectedItems = ListBoxOptions.Items.Cast<CheckBox>().Where(x => x.IsChecked == true).Select(x => x.Content).ToList();
MessageBox.Show(string.Join("\n", selectedItems));
}
}
}
Short explanation:
First of all the Items of the ListBox which are just objects are cast to CheckBoxes as you want to access their Content properties.
Then these CheckBoxes are filtered by IsChecked == true.
Then their Contents which is just an IEnumerable will be converted to a List.
Finally the List will be concatenated with Line Feeds so that the MessageBox will show
e.g.
TEST A
TEST B
If you want to have a comma separated List instead
you can replace the separator like this.
string.Join(",", selectedItems)