Multibinding and IMultiValueConverter in WPF

Introduction

In some cases we might have several fields in your data model that represent a name (first name, middle name and last name) that you want to combine into a single entry in a list. Multibinding takes multiple values and combines them into another value. There are two ways to do multibinding, either using StringFormat or by a converter. The StringFormat is simple compared to a converter, so we will start with that first.

  1. <TextBlock>  
  2.    <TextBlock.Text>  
  3.    <MultiBinding StringFormat=" {0}, {1}, {2}">  
  4.    <Binding ElementName="ThisWindow" Path="FirstName"/>  
  5.    <Binding ElementName="ThisWindow" Path="MiddleName"/>  
  6.    <Binding ElementName="ThisWindow" Path="LastName"/>  
  7.    </MultiBinding>  
  8.    </TextBlock.Text>  
  9. </TextBlock>  
Here the {0} ,{1}, {2} values will be replaced at runtime with FirstName, MiddleName, LastName.

Using Converter

The converter must be derived from IMultiValueConverter and is very similar to a converter derived from IValueConverter, except that instead of having a single value as an argument, it takes multiple arguments.
  1. public class TotalConverter : IMultiValueConverter  
  2.     {  
  3.   
  4.         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  5.         {  
  6.             decimal Amount = 0;  
  7.             decimal Discount = 0;  
  8.             string TotalAmount = string.Empty;  
  9.             Amount = (values[0] != null && values[0] != DependencyProperty.UnsetValue) ? System.Convert.ToDecimal(values[0]) : 0;  
  10.             Discount = (values[0] != null && values[1] != DependencyProperty.UnsetValue) ? System.Convert.ToDecimal(values[1]) : 0;  
  11.             TotalAmount = System.Convert.ToString(Amount - Discount);  
  12.           return TotalAmount;   
  13.         }  
  14.   
  15.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)  
  16.         {  
  17.             throw new NotImplementedException();  
  18.         }  
  19.     }  
Here in the value field we will get multiple values (amount and discount), after conversation the single value will be returned.

Now we have a MultiValue converter, we need to create an instance of it in a XAML resource.

xml code

We put in the main Window tag to remind you to put in the converter namespace. Then we instantiate the converter as a resource with a name. Now we can do our multibinding.
  1. <TextBox Margin="2" MinWidth="120" Grid.Row="1" Grid.Column="1">  
  2.             <TextBox.Text>  
  3.                 <MultiBinding Converter="{StaticResource Totalconverter }">  
  4.                     <Binding Path="Amount"   ElementName="ThisWindow"/>  
  5.                     <Binding Path="Discount"  ElementName="ThisWindow"/>  
  6.                 </MultiBinding>  
  7.             </TextBox.Text>  
  8.         </TextBox>  
Here we have the two properties (Amount and Discount), these properties are bound to the TextBox and implemented TotalConverter.

In MainWindow.xaml.cs:
  1. public partial class MainWindow : Window, INotifyPropertyChanged  
  2.     {  
  3.         public MainWindow()  
  4.         {  
  5.             InitializeComponent();  
  6.         }  
  7.         private decimal amount ;  
  8.   
  9.         public decimal Amount  
  10.         {  
  11.             get { return amount; }  
  12.             set { amount = value;  
  13.             OnPropertyChanged("Amount");  
  14.             }  
  15.         }  
  16.         private decimal discount;  
  17.   
  18.         public decimal Discount  
  19.         {  
  20.             get { return discount; }  
  21.             set { discount = value;  
  22.             OnPropertyChanged("Discount");  
  23.             }  
  24.         }         
  25.          
  26.  
  27.         #region INotifyPropertyChanged Members implementation  
  28.   
  29.         public event PropertyChangedEventHandler PropertyChanged;  
  30.         public void OnPropertyChanged(string txt)  
  31.         {  
  32.   
  33.             PropertyChangedEventHandler handle = PropertyChanged;  
  34.             if (handle != null)  
  35.             {  
  36.                 handle(thisnew PropertyChangedEventArgs(txt));  
  37.             }  
  38.         }  
  39.         #endregion  
  40.     }  
That's it. We made the application. Let me run the application.

enterzero

Figure: Initial state

entervalue

Figure: Amount = 2 and Discount = 0. So Amount – Discount = Total = 2

mainwin

Figure: Amount = 223413 and Discount = 0. So Amount – Discount = Total = 223413

mainwindowenter

Figure: Amount = 223413 and Discount = 564554544. So Amount – Discount = Total = -564331131 

Up Next
    Ebook Download
    View all
    Learn
    View all