How to Bind a Dictionary to a Listbox in Apps Based in XAML

In this article you will learn how to binding a Dictionary to a Listbox in apps based in XAML, using the MVVM pattern.

Introduction

The purpose of this sample is to show how to bind a Dictionary a Lisbox in apps based in XAML, using the MVVM pattern.

Building the Sample

You only need Visual Studio 2012/2013 running in Windows 7, 8, 8.1 or later.

Description

Suppose we have a resource dictionary with some data that we need to show in an application and for each item in a Listbox, we want to show the key/value.

We will start by creating a model class called "Company" that has only two properties: Name and URL.

C#

  1. /// <summary>   
  2. /// Define the Company.   
  3. /// </summary>   
  4. public class Company   
  5. {   
  6.     /// <summary>   
  7.     /// Gets or sets the name.   
  8.     /// </summary>   
  9.     /// <value>The name.</value>   
  10.     public string Name { getset; }   
  11.   
  12.     /// <summary>   
  13.     /// Gets or sets the URL.   
  14.     /// </summary>   
  15.     /// <value>The URL.</value>   
  16.     public string Url { getset; }   
  17. }  
In the MainViewModel we will create the resource dictionary where the Company is the key and the value will be an int and we will have a SelectedCompany property to get the company selected in the listbox.

The MainViewModel will be defined by:

C#
  1. public class MainViewModel : ViewModelBase   
  2. {   
  3.    private Company _selectedCompany;   
  4.   
  5.    /// <summary>   
  6.    /// Initializes a new instance of the MainViewModel class.   
  7.    /// </summary>   
  8.    public MainViewModel()   
  9.    {   
  10.       Companies = new Dictionary<Company, int>   
  11.       {   
  12.          {   
  13.             new Company   
  14.             {   
  15.                Name = "Microsoft", Url="www.microsoft.com"   
  16.             }, 1   
  17.          },   
  18.          {   
  19.             new Company   
  20.             {   
  21.                Name = "Google", Url="www.google.com"   
  22.             }, 2   
  23.          },   
  24.          {   
  25.             new Company   
  26.             {   
  27.                Name = "Apple", Url="www.apple.com"   
  28.             }, 3   
  29.          }   
  30.       };   
  31.    }   
  32.   
  33.    /// <summary>   
  34.    /// Gets or sets the selected company.   
  35.    /// </summary>   
  36.    /// <value>The selected company.</value>   
  37.    public Company SelectedCompany   
  38.    {   
  39.       get { return _selectedCompany; }   
  40.       set { Set(() => SelectedCompany, ref _selectedCompany, value); }   
  41.    }   
  42.   
  43.    /// <summary>   
  44.    /// Gets or sets the companies.   
  45.    /// </summary>   
  46.    /// <value>The companies.</value>   
  47.    public Dictionary<Company, int> Companies { getset; }   
  48. }  
The MainWindow will defined as in the following:

XAML
  1. <mui:ModernWindow x:Class="BindingResourceDictionarySample.MainWindow"   
  2.                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.                   xmlns:mui="http://firstfloorsoftware.com/ModernUI"   
  5.                   Title="Sample"   
  6.                   Width="525"   
  7.                   Height="350"   
  8.                   DataContext="{Binding Main,   
  9.                                         Source={StaticResource Locator}}"   
  10.                   Style="{StaticResource BlankWindow}">   
  11.     <StackPanel>   
  12.         <TextBlock Margin="20,20,0,0" Text="How to binding a Dictionary to a Listbox" />   
  13.         <ListBox Width="250"   
  14.                  Margin="20,20,0,0"   
  15.                  HorizontalAlignment="Left"   
  16.                  ItemsSource="{Binding Companies}"   
  17.                  SelectedValue="{Binding ItemIndex}"   
  18.                  SelectedValuePath="Key"   
  19.                  SelectionMode="Single">   
  20.             <ListBox.ItemTemplate>   
  21.                 <DataTemplate>   
  22.                     <Border Width="245"   
  23.                             BorderBrush="Orange"   
  24.                             BorderThickness="2">   
  25.                         <StackPanel Orientation="Horizontal">   
  26.                             <TextBlock Margin="20,0,0,0" Text="{Binding Path=Value}" />   
  27.                             <TextBlock Margin="20,0,0,0" Text="{Binding Path=Key.Name}" />   
  28.                         </StackPanel>   
  29.                     </Border>   
  30.                 </DataTemplate>   
  31.             </ListBox.ItemTemplate>   
  32.         </ListBox>   
  33.     </StackPanel>   
  34. </mui:ModernWindow>   
To get the selected Company the SelectedValue property from the Listbox will be used that will use the SelectedValuePath to understand which value to return. To get the value instead of the Company we should change the Key to Value.

Note:   
  1. To have a nice look, we will use the ModernWindow from the ModernUI (for WPF). For more see the ModerUI in Modern UI Samples.

  2. The sample is similar to any XAML app, where we show the UI for WPF.

Running the sample

binding a dictonary

Source code files:

 

  • ViewModelLocator class contains static references to all the view model in the application and provide an entry point for the bindings.

  • MainViewModel class for binding to MainView.xaml

  • MainWindow represents the view.

  • Company defines the model.

Source Code

The source code can be found in the MSDN Samples.