MultiBinding Class in WPF

In this simple example we will see how to use the Class MultiBinding. The class MultiBinding allows you to associate a target property of the association with a list of source properties and then apply logic to produce a value with the inputs specified. This example shows how to use the class MultiBinding to display multiple properties of the Customer class in a single line in a ListBox.

We will use an ObservableCollection that is a collection of dynamic data that provides notifications when items are added, removed, or if the list of everything is updated.

Then we implement code bheind to implement a class called Customer, composed in turn by five properties, but we see the C# code.

  1. using System.Windows;  
  2. using System.Collections.ObjectModel;  
  3.   
  4. namespace WpfApplication1  
  5. {  
  6.     /// <summary>  
  7.     /// Logica di interazione per MainWindow.xaml  
  8.     /// </summary>  
  9.     public partial class MainWindow : Window  
  10.     {  
  11.         public MainWindow()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.   
  16.         private void ButtonClick(object sender, RoutedEventArgs e)  
  17.         {  
  18.             /* Creiamo una ObservableCollection di tipo Customer 
  19.             * andando a valorizzare le sue proprietà*/  
  20.             var customerList = new ObservableCollection<Customer>  
  21.             {  
  22.                 new Customer  
  23.                 {  
  24.                     Name = "Carmelo",  
  25.                     Surname = "La Monica",  
  26.                     Address = "indirizzo",  
  27.                     Email = "email",  
  28.                     PhoneNumber = "telefono",  
  29.                 }  
  30.             };  
  31.   
  32.             /* Assegniamo alla proprietà ItemSource del controllo ListBox 
  33.             * il contenuto di customerlist*/  
  34.             listbox1.ItemsSource = customerList;  
  35.         }  
  36.   
  37.         // La classe Customer con le cinque proprietà  
  38.         private class Customer  
  39.         {  
  40.             public string Name{ getset; }  
  41.             public string Surname{ getset; }  
  42.             public string PhoneNumber{ getset; }  
  43.             public string Address{ getset; }  
  44.             public string Email{ getset; }  
  45.         }  
  46.     }  
  47. }  

The objective is to see the name and surname of a person for each line inside a ListBox. Since the property DisplayMemberPath allows us to see only one property at a time, not two, here we use MultiBinding, but let's see how to implement it in the declarative XAML code.

  1. <Window x : Class = "WpfApplication1.MainWindow"  
  2.     xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation This link is external to TechNet Wiki. It will open in a new window. "  
  3. xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml This link is external to TechNet Wiki. It will open in a new window. "  
  4.         Title = "MainWindow" Height = "350" Width = "525">  
  5.     <Grid>  
  6.         <ListBox x : Name = "listbox1" IsSynchronizedWithCurrentItem = "True"  
  7.         HorizontalAlignment = "Left" Height = "100" Margin = "63,49,0,0" VerticalAlignment = "Top" Width = "213">  
  8.             <ListBox.ItemTemplate>  
  9.                 <DataTemplate>  
  10.                     <TextBlock>  
  11.                         <TextBlock.Text>  
  12.                             <MultiBinding StringFormat = "{}{0} {1}">  
  13.                                 <Binding Path = "Name" / >  
  14.                                 <Binding Path = "Surname" / >  
  15.                             </ MultiBinding>  
  16.                         </ TextBlock.Text>  
  17.                     </ TextBlock>  
  18.                 </ DataTemplate>  
  19.             </ ListBox.ItemTemplate>  
  20.         </ ListBox>  
  21.                                 <Button Content = "Button" HorizontalAlignment = "Left" Margin = "63,178,0,0" VerticalAlignment = "Top" Width = "75" Click = "ButtonClick" / >  
  22.     </ Grid  
  23. </ Window>  

We define a ListBox and create an ItemTemplate in it and note we have included a control TextBock, the latter implements the MultiBinding so that we can define what properties we want to see then, perform the formatting of the display StringFormat = "{} {0} {1}" to display the Name and Surname of the Customer class on the same line. By running this code on pressing the button Button1 we have this situation.

Here's how it will be displayed in the ListBox value composed of two properties of the Customer class, without the MultiBinding we had to resort to other means, such as defining a property FirstnameLastname in the Customer class and we enhance it with the name, then assign it or bind it to the ListBox in XAML, or enhance it by code bheind through ownership DisplayMemberPath.

Up Next
    Ebook Download
    View all
    Learn
    View all