How to get checked items in a WPF ListBox?

Posted by Joan Venge on Stack Overflow See other posts from Stack Overflow or by Joan Venge
Published on 2010-03-26T00:19:03Z Indexed on 2010/03/26 0:23 UTC
Read the original article Hit count: 383

Filed under:
|
|

I have a WPF ListBox where I have checkboxes, but what's the way to get the list of items that are checked?

The ListBox is data binded to a Dictionary<T>.

Here is the XAML:

<Window x:Class="WpfApplication.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1"
        Height="300"
        Width="300">
    <Grid Margin="10">
        <ListBox ItemsSource="{DynamicResource Nodes}" Grid.IsSharedSizeScope="True" x:Name="MyList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="Key" />
                            <ColumnDefinition SharedSizeGroup="Name" />
                            <ColumnDefinition SharedSizeGroup="Id" />
                        </Grid.ColumnDefinitions>
                        <CheckBox Name="NodeItem" Click="OnItemChecked">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Margin="2" Text="{Binding Value.Name}" Grid.Column="1"/>
                                <TextBlock Margin="2" Text="-" Grid.Column="2"/>
                                <TextBlock Margin="2" Text="{Binding Value.Id}" Grid.Column="3"/>
                            </StackPanel>
                        </CheckBox>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET