List box that uses data binding to populate the list box items.

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">
      <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;
        }

    }

}

   
    
     


This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.

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">
      <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;
        }

    }

}

   
    
     


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();
        }
    }

}

   
    
     


Get selected item count from ListBox

image_pdfimage_print


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="AboutDialog" ListBox.SelectionChanged="ListBox_SelectionChanged"
        Button.Click="Button_Click"
        Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
        Background="OrangeRed">
  <StackPanel>
    <Label FontWeight="Bold" FontSize="20" Foreground="White">
      WPF
    </Label>
    <Label>License</Label>
    <Label>Installed Dll:</Label>
    <ListBox>
      <ListBoxItem>1</ListBoxItem>
      <ListBoxItem>2</ListBoxItem>
      <ListBoxItem>3</ListBoxItem>
      <ListBoxItem>4</ListBoxItem>
      <ListBoxItem>5</ListBoxItem>
      <ListBoxItem>6</ListBoxItem>
      <ListBoxItem>7</ListBoxItem>
      <ListBoxItem>8</ListBoxItem>
      <ListBoxItem>9</ListBoxItem>
    </ListBox>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10">Help</Button>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>test</StatusBar>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

public partial class AboutDialog : Window
{
    public AboutDialog()
    {
        InitializeComponent();
    }

    void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
            MessageBox.Show("You just selected " + e.AddedItems[0]);
    }

    void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("You just clicked " + e.Source);
    }
}

   
    
     


ListBox With Items Panel

image_pdfimage_print


   
      
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ListBox HorizontalAlignment="Center" VerticalAlignment="Center">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBoxItem>Whatever Item 1</ListBoxItem>
        <ListBoxItem>Whatever Item 2</ListBoxItem>
        <ListBoxItem>Whatever Item 3</ListBoxItem>
        <ListBoxItem>Whatever Item 4</ListBoxItem>
        <ListBoxItem>Whatever Item 5</ListBoxItem>
        <ListBoxItem>Whatever Item 6</ListBoxItem>
        <ListBoxItem>Whatever Item 7</ListBoxItem>
        <ListBoxItem>Whatever Item 8</ListBoxItem>
        <ListBoxItem>Whatever Item 9</ListBoxItem>

    </ListBox>
</Page>

   
    
    
    
    
    
     


Rotate a ListBox

image_pdfimage_print


   
      

<Window x:Class="WPFTransformations.Rotate"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Rotate Transform" Height="350" Width="300">
  <Grid>
        <StackPanel Margin="8">
            <TextBlock Height="25" Width="100"/>
            <ListBox Height="100" Width="200" BorderThickness="2">
                <ListBoxItem Content="Item 1" Height="22"/>
                <ListBoxItem Content="Item 2" Height="22"/>
                <ListBoxItem Content="Item 3" Height="22"/>
                <ListBoxItem Content="Item 4" Height="22"/>
                <ListBox.RenderTransform>
                   <RotateTransform
                        Angle="{Binding ElementName=sliderAngle, Path=Value}" 
                        CenterX="{Binding ElementName=sliderCenterX, Path=Value}" 
                        CenterY="{Binding ElementName=sliderCenterY, Path=Value}"/>
                </ListBox.RenderTransform>
            </ListBox>

            <TextBlock Height="65" Width="100"/>
            <Grid HorizontalAlignment="Center" Margin="2">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="110"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Text="Angle:"/>
                <Slider Grid.Row="0" Grid.Column="1" Name="sliderAngle" Maximum="360" Minimum="-360"/>
                <TextBox Grid.Row="0" Grid.Column="2" Text="{Binding ElementName=sliderAngle, Path=Value}"/>

                <TextBlock Grid.Row="2" Grid.Column="0" Text="Center Y:"/>
                <Slider Grid.Row="2" Grid.Column="1" Name="sliderCenterY"/>
                <TextBox Grid.Row="2" Grid.Column="2" Text="{Binding ElementName=sliderCenterY, Path=Value}"/>

            </Grid>
        </StackPanel>
        <StackPanel Margin="8">
            <TextBlock Height="25" Width="100"/>
            <Rectangle Height="100" Width="200" Stroke="Red" Fill="Red" Opacity=".05"/>
        </StackPanel>
    </Grid>
</Window>

   
    
    
    
    
    
     


Scale a ListBox

image_pdfimage_print


   
      
<Window x:Class="WPFTransformations.Scale"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Scale Transform" Height="400" Width="300"
  >

    <Grid>
        <StackPanel Margin="8">
            <TextBlock Height="25" Width="100"/>

            <ListBox Height="100" Width="200" BorderThickness="2">
                <ListBoxItem Content="Item 1" Height="22"/>
                <ListBoxItem Content="Item 2" Height="22"/>
                <ListBoxItem Content="Item 3" Height="22"/>
                <ListBoxItem Content="Item 4" Height="22"/>
                <ListBox.RenderTransform>
                    <ScaleTransform ScaleX="{Binding Path=Value, ElementName=sliderScaleX}" 
                                    ScaleY="{Binding Path=Value, ElementName=sliderScaleY}"
            CenterX="{Binding Path=Value, ElementName=sliderScaleCX}" 
            CenterY="{Binding Path=Value, ElementName=sliderScaleCY}" 
            />
                </ListBox.RenderTransform>
            </ListBox>

            <TextBlock Height="65" Width="100"/>
            <Grid HorizontalAlignment="Center" Margin="2">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="62" />
                    <ColumnDefinition Width="93"/>
                    <ColumnDefinition Width="16*" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="Scale X:" Margin="0,0,16.323,0" />
                <Slider Name="sliderScaleX" Maximum="2.5" Minimum="0" Value="1" TickFrequency=".1" Grid.ColumnSpan="2" Margin="45.677,0,0,0" />
                <TextBox Grid.Row="0" Grid.Column="2" Text="{Binding Path=Value, ElementName=sliderScaleX}"/>

                <TextBlock Grid.Row="1" Text="Scale Y:" Margin="0,0,16.323,0" />
                <Slider Grid.Row="1" Name="sliderScaleY" Maximum="2.5" Minimum="0" Value="1" TickFrequency=".1" Grid.ColumnSpan="2" Margin="45.677,0,0,0" />
                <TextBox Grid.Row="1" Grid.Column="2" Text="{Binding Path=Value, ElementName=sliderScaleY}"/>

            </Grid>
        </StackPanel>
        <StackPanel Margin="8">
            <TextBlock Height="25" Width="100"/>
            <Rectangle Height="100" Width="200" Stroke="Red" Fill="Red" Opacity=".05"/>
        </StackPanel>
    </Grid>
</Window>