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. public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  4. {
  5. decimal Amount = 0;
  6. decimal Discount = 0;
  7. string TotalAmount = string.Empty;
  8. Amount = (values[0] != null && values[0] != DependencyProperty.UnsetValue) ? System.Convert.ToDecimal(values[0]) : 0;
  9. Discount = (values[0] != null && values[1] != DependencyProperty.UnsetValue) ? System.Convert.ToDecimal(values[1]) : 0;
  10. TotalAmount = System.Convert.ToString(Amount - Discount);
  11. return TotalAmount;
  12. }
  13. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  14. {
  15. throw new NotImplementedException();
  16. }
  17. }
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. public decimal Amount
  9. {
  10. get { return amount; }
  11. set { amount = value;
  12. OnPropertyChanged("Amount");
  13. }
  14. }
  15. private decimal discount;
  16. public decimal Discount
  17. {
  18. get { return discount; }
  19. set { discount = value;
  20. OnPropertyChanged("Discount");
  21. }
  22. }
  23. #region INotifyPropertyChanged Members implementation
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. public void OnPropertyChanged(string txt)
  26. {
  27. PropertyChangedEventHandler handle = PropertyChanged;
  28. if (handle != null)
  29. {
  30. handle(this, new PropertyChangedEventArgs(txt));
  31. }
  32. }
  33. #endregion
  34. }
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