What’s New In Silverlight 3 – Multi-Select List Box

n Silverlight 3 the standard ListBox now has a SelectionMode property which is filled from an enumerated constant whose possible values are

•    Single
•    Multiple
•    Extended

When setting the SelectionMode Intellisense will display the potential values

MSListBox

Creating and using a multi-select list box requires only setting the property and retrieving the values. In the following example we create a multi-select list box, a button to indicate that our selection is complete, and a TextBlock to display the retrieved values,

   1: <UserControl x:Class="MultiSelectLB.MainPage"

   2:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   3:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   4:   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

   5:   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   6:   mc:Ignorable="d"

   7:   Width="500"

   8:   Height="280">

   9:   <Grid x:Name="LayoutRoot"

  10:         Background="wheat"

  11:         Margin="10">

  12:     <Grid.ColumnDefinitions>

  13:       <ColumnDefinition Width="1*" />

  14:       <ColumnDefinition Width="1*" />

  15:     </Grid.ColumnDefinitions>

  16:     <Grid.RowDefinitions>

  17:       <RowDefinition Height="1.5*" />

  18:       <RowDefinition Height="1*" />

  19:       <RowDefinition Height="1*" />

  20:       <RowDefinition Height="*" />

  21:     </Grid.RowDefinitions>

  22:

  23:     <TextBlock x:Name="Title"

  24:       Margin="0"

  25:       Text="Multi-select List Box"

  26:       Foreground="Red"

  27:       FontFamily="Georgia"

  28:       FontSize="32"

  29:       HorizontalAlignment="Center"

  30:       VerticalAlignment="Center"

  31:       Grid.ColumnSpan="2" />

  32:

  33:     <ListBox x:Name="msLB"

  34:       Margin="5"

  35:       Grid.Row="1"

  36:       Grid.Column="0"

  37:       SelectionMode="Multiple" />

  38:

  39:     <Button x:Name="readyButton"

  40:        Margin="5"

  41:        Grid.Row="1"

  42:        Grid.Column="1"

  43:        Width="100"

  44:        Height="35"

  45:        Content="Click when ready" />

  46:

  47:     <TextBlock x:Name="Message"

  48:         HorizontalAlignment="Left"

  49:         Margin="5"

  50:         Grid.Row="2"

  51:         Grid.Column="0"

  52:         Grid.ColumnSpan="2"

  53:         Foreground="Blue"

  54:         VerticalAlignment="Bottom"

  55:         FontFamily="Georgia"

  56:         FontSize="18" />

  57:

  58:   </Grid>

  59: </UserControl>

Note the selection mode set on line 37.

We need an event handler for clicking the button, that will find all the selected items and stringify them for the Message TextBlock:

   1: using System.Collections.Generic;

   2: using System.Windows.Controls;

   3:

   4: namespace MultiSelectLB

   5: {

   6:    public partial class MainPage : UserControl

   7:    {

   8:       private readonly List<string> presidents =

   9:          new List<string> { "Washington", "Lincoln", "FDR", "Kennedy", "Obama" };

  10:       public MainPage()

  11:       {

  12:          InitializeComponent();

  13:          msLB.ItemsSource = presidents;

  14:          readyButton.Click += BClick;

  15:       }

  16:

  17:       private void BClick(object sender, System.Windows.RoutedEventArgs e)

  18:       {

  19:          var selectedPresidents = msLB.SelectedItems;

  20:          Message.Text = string.Empty;

  21:          foreach (string presidentName in selectedPresidents)

  22:          {

  23:             if (Message.Text.Length > 0)

  24:             {

  25:                Message.Text += ", ";

  26:             }

  27:             Message.Text += presidentName;

  28:          }     // end foreach

  29:       }        // end BClick

  30:    }           // end class

  31: }              // end namespace

As an aside, not that using var for the type in line 19 allows the program to remain type safe without the programmer having to find the specific enumerable that the SelectedItems property will return. Sweet.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.