What’s New In Silverlight 3 - Multi-Select List Box - Jesse Liberty - Silverlight Geek Page view counter

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.

Here’s a working example:

Previous Element Binding             Next: Merged Resources
Published Wednesday, July 22, 2009 2:03 PM by jesseliberty

Comments

# What???s New In Silverlight 3 ??? Element Binding - Jesse Liberty - Silverlight Geek

Pingback from  What???s New In Silverlight 3 ??? Element Binding - Jesse Liberty - Silverlight Geek

# What's New In Silverlight 3 - Jesse Liberty - Silverlight Geek

Pingback from  What's New In Silverlight 3 - Jesse Liberty - Silverlight Geek

# re: What’s New In Silverlight 3 - Multi-Select List Box

"var" variables are figured out at compile time, not at runtime. So regardless of the type of objects that you are populating the list box with, the SelectedItems property is always a System.Collections.IList. In fact, you don't even really need to declare selectedPresidents, you could have just written:

foreach(string presidentName in msLB.SelectedItems)

Wednesday, July 22, 2009 2:57 PM by jackbond

# re: What’s New In Silverlight 3 - Multi-Select List Box

jackbond, yes, you are certainly correct: compile time (which is in large measure what type-safe implies), and yes I could have left out the interim object - though only if I wanted to use them in this way.

However, when you write that the SelectedItems property is always a System.Collections.IList, you seem to be saying that because it is at compile time that is a given; I think my point was this: what you know about foreach is that you need an array or something implements IEnumerable.

Using var here lets you make clear that you'll take any collection that does so, without having to know that what you actually have is a collection that implements IList, [which in turn extends IEnumerable (and ICollection)].

So, just to beat on this poor dead subject a bit longer, (1) compile time, absolutely

(2) "So, regardless..."  - I don't think the so (implying cause/effect) works, and without that, the fact that it is always an IList doesn't (imho) change my point

(3) I personally would use the temp variable for any number of reasons including debugging.

But now I've made Mt. Everest out of this, so apologies for getting carried away. :-)

Wednesday, July 22, 2009 4:14 PM by jesseliberty

# re: What’s New In Silverlight 3 - Multi-Select List Box

Well to beat the horse a little further... The point I was trying to get across is that:

var whatever = //unless this is an anonymous type, using var is simply a shortcut

When you say:

"using var for the type in line 19 allows the program to remain type safe without the programmer having to find the specific enumerable"

Seems to imply that something is going on at runtime. In other words, how is using "var" any different than using "System.Collections.IList" (except for brevity?) In reality, the compiler is doing the swap for you.

Obviously having the interim variable selectPresidents could be useful (point 3), but your usage of 'var' hasn't added any type safety. In fact your foreach loop isn't type safe. It assumes that the contained type is string (obviously a pretty safe assumption.) But if you really wanted to add type safety, then you should do:

IEnumerable<string> selectedPresidents = msLB.SelectedItems.OfType<string>();

One last question, why is your comment box so narrow? It's got a lot of whitespace to the right that it could be using.

Wednesday, July 22, 2009 4:49 PM by jackbond

# re: What’s New In Silverlight 3 - Multi-Select List Box

it will b very good if we can use silverlight in vs2005

Thursday, July 23, 2009 5:56 AM by jewelhere

# What’s New In Silverlight 3 - Multi-Select List Box - Jesse Liberty

Thank you for submitting this cool story - Trackback from DotNetShoutout

Thursday, July 23, 2009 6:34 PM by DotNetShoutout

# What’s New In Silverlight 3 – Merged Resources

Silverlight 3 provides the ability to manage your various resources in dedicated files and folders, and

Monday, July 27, 2009 12:38 PM by Microsoft Weblogs

# What???s New In Silverlight 3 ??? Merged Resources - Jesse Liberty - Silverlight Geek

Pingback from  What???s New In Silverlight 3 ??? Merged Resources - Jesse Liberty - Silverlight Geek

# ??????????????? 3??? ????????? ??????(??????) &laquo; jin_u as blog

Pingback from  ??????????????? 3??? ????????? ??????(??????) &laquo; jin_u as blog

# Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in SL 3?

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in SL 3?