This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key. : ListBox « Windows Presentation Foundation « C# / CSharp Tutorial






<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">
      <WrapPanel Width="500" Orientation="Horizontal" Name="rectanglesPanel">
        <WrapPanel.Resources>
          <Style TargetType="Rectangle">
            <Setter Property="Height" Value="20"/>
            <Setter Property="Width" Value="20"/>
            <Setter Property="Margin" Value="5"/>
          </Style>
        </WrapPanel.Resources>
      </WrapPanel>
      <ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended" 
            Width="265" Height="55" Background="HoneyDew" SelectionChanged="myListBox_SelectionChanged"
            ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">
      </ListBox>
    </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 myListBox_SelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            BrushConverter converter = new BrushConverter();
            foreach (string color in args.AddedItems)
            {
                if (GetRectangle(color) == null)
                {
                    Rectangle aRect = new Rectangle();
                    aRect.Fill = (Brush) converter.ConvertFrom(color);
                    aRect.Tag = color;
                    rectanglesPanel.Children.Add(aRect);
                }

            }
            foreach (string color in args.RemovedItems)
            {
                FrameworkElement removedItem = GetRectangle(color);
                if (removedItem != null)
                {
                    rectanglesPanel.Children.Remove(removedItem);
                }
            }
        }

        FrameworkElement GetRectangle(string color)
        {
            foreach (FrameworkElement rect in rectanglesPanel.Children)
            {
                if (rect.Tag.ToString() == color)
                    return rect;
            }

            return null;
        }

    }

}
WPF This List Box Allows Items To Be Selected In Groups By Using The S H I F T Key And Mouse Or The C T R L Key And Space Key








24.29.ListBox
24.29.1.ListBox and SelectionModeListBox and SelectionMode
24.29.2.ListBox Selected Index, Item, ValueListBox Selected Index, Item, Value
24.29.3.ListBox SelectionMode=SingleListBox SelectionMode=Single
24.29.4.Get Selected Item from ListBoxGet Selected Item from ListBox
24.29.5.Load the Items in a ListBox AsynchronouslyLoad the Items in a ListBox Asynchronously
24.29.6.ListBox selection changed eventListBox selection changed event
24.29.7.Handles ListBoxItem Selected events for the ListBoxItems in the inner ListBox.Handles ListBoxItem Selected events for the ListBoxItems in the inner ListBox.
24.29.8.Select All ListBox ItemsSelect All ListBox Items
24.29.9.Add selected file to ListBoxAdd selected file to ListBox
24.29.10.Fill up the ListBox with brush namesFill up the ListBox with brush names
24.29.11.Get selected item from ListBoxGet selected item from ListBox
24.29.12.This list box allows multiple user selections.This list box allows multiple user selections.
24.29.13.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.
24.29.14.Select All and unselect allSelect All and unselect all
24.29.15.Get selected item count from ListBoxGet selected item count from ListBox
24.29.16.View and Select Items Using a ListView and Select Items Using a List
24.29.17.Set text to TextBlock for selected list itemSet text to TextBlock for selected list item
24.29.18.Iterate through the selected items and remove each oneIterate through the selected items and remove each one
24.29.19.Ensure there is at least one item selectedEnsure there is at least one item selected