Select All and unselect all

image_pdfimage_print


   
  

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ListBoxEvent"  
    x:Class="ListBoxEvent.Pane1">
  <Canvas.Resources>
    <src:myColors x:Key="Colors"/>
  </Canvas.Resources>

    <StackPanel Margin="10, 10, 3, 3">
      <ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended" 
            Width="265" Height="55"
            ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">
      </ListBox>
      <StackPanel Orientation="Horizontal" Margin="10">
        <Button Content="_Select all" Click="selectAll_Click"/>
        <Button Content="_Unselect all" Click="unselectAll_Click"/>
      </StackPanel>
    </StackPanel>


</Canvas>
//File:Window.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace ListBoxEvent
{
    public class myColors : ObservableCollection<string>
    {
        public myColors()
        {
            Add("LightBlue");
            Add("Pink");
            Add("Red");
            Add("Purple");
            Add("Blue");
            Add("Green");
        }
    }
    public partial class Pane1 : Canvas
    {
        public Pane1() : base()
        {
            InitializeComponent();
        }
        void selectAll_Click(object sender, RoutedEventArgs e)
        {
            myListBox.SelectAll();
        }

        void unselectAll_Click(object sender, RoutedEventArgs e)
        {
            myListBox.UnselectAll();
        }
    }

}