Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Listbox with checkboxes multiple checking

Tags:

c#

.net

wpf

I've got a WPF listbox with checkboxes added to it, and at the moment it looks like this: WPF checkbox

To select all the different items I have to click each checkbox one by one, or do a select all (which I have a separate button for). But if I want to select only half, then it is painful.

What I'd like to be able to do is click one, hold shift, click another and then click the checkbox next to one of them to toggle all those selected. Windows Forms allows this pretty easily I think, but I'm not sure what to do in WPF? At the moment I've got it set to only allow selecting of one at a time (selection means nothing, its all about the checks).

Ideally I'd also have it so selecting something checks it (ie instead of having to pick out the small checkbox you can click the words) but I think that may be hard to do with my shift+select thing.

    <Window.Resources>
    <DataTemplate x:Key="ListBoxItemTemplate" >

        <WrapPanel>
            <CheckBox Focusable="False" IsChecked="{Binding Selected}" VerticalAlignment="Center" />
            <ContentPresenter Content="{Binding Name, Mode=OneTime}"  Margin="2,0" />
        </WrapPanel>

    </DataTemplate>

</Window.Resources>

<ListBox Margin="10" HorizontalAlignment="Stretch" Name="lbSheets" 
                 VerticalAlignment="Stretch" Width="Auto" Grid.Row="1" MinWidth="321"
                 MinHeight="40" HorizontalContentAlignment="Left" 
                 ItemTemplate="{StaticResource ListBoxItemTemplate}" VerticalContentAlignment="Top" Background="#FFDCEBEE" SelectionMode="Single">

</ListBox>

I hope this all makes sense - what is the best way to do this in WPF?

like image 680
RodH257 Avatar asked Feb 28 '11 01:02

RodH257


1 Answers

Check out the SelectionMode Property. Notice that when in Extended mode, you can Shift-Click groups of items, by clicking over the ListBoxItem text or CheckBox.Read the below article and you will get a better idea

http://www.codeproject.com/KB/WPF/WPFProblemSolving.aspx

like image 148
biju Avatar answered Oct 14 '22 10:10

biju