0

Can I select all items in WPF Listbox through XAML only?

I followed this question as well WPF Listbox and Select All but it's not working. My XAML code as follows:

<ListBox SelectionMode="Multiple" Name="listBox">
    <ListBox.InputBindings>
        <KeyBinding Command="ApplicationCommands.SelectAll" Modifiers="Ctrl" Key="A" />
    </ListBox.InputBindings>
    <ListBox.CommandBindings>
        <CommandBinding Command="ApplicationCommands.SelectAll"  />
    </ListBox.CommandBindings>
    <ListBoxItem>List Item 1</ListBoxItem>
    <ListBoxItem>List Item 2</ListBoxItem>
    <ListBoxItem>List Item 3</ListBoxItem>
    <ListBoxItem>List Item 4</ListBoxItem>
    <ListBoxItem>List Item 5</ListBoxItem>
</ListBox>

Is Select all possible through XAMl only?

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
Ravi Anand
  • 5,106
  • 9
  • 47
  • 77

1 Answers1

1

No, the ListBox has its own SelectAllCommand but it is private so you can't bind to it. It doesn't handle the ApplicationCommands.SelectAll command for you. You need to do this yourself by handling the Executed event of the CommandBinding:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    listBox.SelectAll();
}

XAML is a markup language and not a programming language.

mm8
  • 163,881
  • 10
  • 57
  • 88