Posted by: Zeeshan Amjad | May 20, 2014

Using Tuple in ListBox


Tuple is a very handy generic utility class in .net that be very useful specially in lookup data situation. Let’s work on a simple example of displaying currency in the list box. In official ISO 4217 standard of currency code, there are currency code, number and the currency name. We can store this information in Tuple.

Code Snippet
public ObservableCollection<Tuple<string, int, string>> Currencies =
    new ObservableCollection<Tuple<string, int, string>>();

 

Now let’s store some currencies information in it.

Code Snippet
Currencies.Add(new Tuple<string, int, string>("AED", 784, "United Arab Emirates"));
Currencies.Add(new Tuple<string, int, string>("AUD", 036, "Australian Dollar"));
Currencies.Add(new Tuple<string, int, string>("CAD", 124, "Canadian Dollar"));
Currencies.Add(new Tuple<string, int, string>("EUR", 978, "Euro"));
Currencies.Add(new Tuple<string, int, string>("GBP", 826, "Pound Sterling"));
Currencies.Add(new Tuple<string, int, string>("JPY", 392, "Japanese Yen"));
Currencies.Add(new Tuple<string, int, string>("MXN", 484, "Mexican Peso"));
Currencies.Add(new Tuple<string, int, string>("NZD", 554, "New Zeland Dollar"));
Currencies.Add(new Tuple<string, int, string>("QAR", 634, "Qatar Riyal"));
Currencies.Add(new Tuple<string, int, string>("SAR", 682, "Saudi Riyal"));
Currencies.Add(new Tuple<string, int, string>("USD", 840, "US Dollar"));

 

Using of Tuple in Listbox is very straight forward. We just bind Item1, Item2 etc in the Listbox. Here is complete XAML code of the program.

Code Snippet
<Window x:Class="WpfListBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        Title="Tuple in Listbox" Height="300" Width="400">
    <Grid>
        <ListBox Margin="5,10,4.6,0.4" ItemsSource="{Binding}"
                 HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding Item1}"/>
                        <TextBlock Grid.Column="1" Text="{Binding Item2}"/>
                        <TextBlock Grid.Column="2" Text="{Binding Item3}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

 

Here is complete C# code of the program.

Code Snippet
using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfListBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Tuple<string, int, string>> Currencies =
            new ObservableCollection<Tuple<string, int, string>>();

        public MainWindow()
        {
            InitializeComponent();

            Currencies.Add(new Tuple<string, int, string>("AED", 784, "United Arab Emirates"));
            Currencies.Add(new Tuple<string, int, string>("AUD", 036, "Australian Dollar"));
            Currencies.Add(new Tuple<string, int, string>("CAD", 124, "Canadian Dollar"));
            Currencies.Add(new Tuple<string, int, string>("EUR", 978, "Euro"));
            Currencies.Add(new Tuple<string, int, string>("GBP", 826, "Pound Sterling"));
            Currencies.Add(new Tuple<string, int, string>("JPY", 392, "Japanese Yen"));
            Currencies.Add(new Tuple<string, int, string>("MXN", 484, "Mexican Peso"));
            Currencies.Add(new Tuple<string, int, string>("NZD", 554, "New Zeland Dollar"));
            Currencies.Add(new Tuple<string, int, string>("QAR", 634, "Qatar Riyal"));
            Currencies.Add(new Tuple<string, int, string>("SAR", 682, "Saudi Riyal"));
            Currencies.Add(new Tuple<string, int, string>("USD", 840, "US Dollar"));

            DataContext = Currencies;
        }
    }
}

 

Here is the output of the program.

TupleListBox

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: