开发者

WPF Listbox with checkboxes multiple checking

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

WPF Listbox with checkboxes multiple checking

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 - wha开发者_C百科t is the best way to do this in WPF?


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


You need to use Extended mode for selecting with Shift. You can use next code to allow checking selected items by Space button (native behaviour of such lists):

private void listBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (!e.IsRepeat && e.Key == Key.Space)
    {
        bool notMixed = listBox.SelectedItems.Cast<object>().Any(item => ((yourItemsClass)item).Selected) ^
                        listBox.SelectedItems.Cast<object>().Any(item => !((yourItemsClass)item).Selected);

        foreach (var item in listBox.SelectedItems)
        {
            yourItemsClass t = (yourItemsClass)item;
            t.Selected = notMixed ? !yourItemsClass.Selected : true;
        }
    }
}

In this example: yourItemsClass is class of source objects which you bind to list. First, you look what items are already checked (notMixed variable). If nothing or some items from selected range have been checked then after Space pressing you just checking rest of items. If all selected items are checked then unchecking them all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜