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.
- using System.Windows;
- using System.Collections.ObjectModel;
-
- namespace WpfApplication1
- {
-
-
-
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void ButtonClick(object sender, RoutedEventArgs e)
- {
-
-
- var customerList = new ObservableCollection<Customer>
- {
- new Customer
- {
- Name = "Carmelo",
- Surname = "La Monica",
- Address = "indirizzo",
- Email = "email",
- PhoneNumber = "telefono",
- }
- };
-
-
-
- listbox1.ItemsSource = customerList;
- }
-
-
- private class Customer
- {
- public string Name{ get; set; }
- public string Surname{ get; set; }
- public string PhoneNumber{ get; set; }
- public string Address{ get; set; }
- public string Email{ get; set; }
- }
- }
- }
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.
- <Window x : Class = "WpfApplication1.MainWindow"
- xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation This link is external to TechNet Wiki. It will open in a new window. "
- xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml This link is external to TechNet Wiki. It will open in a new window. "
- Title = "MainWindow" Height = "350" Width = "525">
- <Grid>
- <ListBox x : Name = "listbox1" IsSynchronizedWithCurrentItem = "True"
- HorizontalAlignment = "Left" Height = "100" Margin = "63,49,0,0" VerticalAlignment = "Top" Width = "213">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <TextBlock>
- <TextBlock.Text>
- <MultiBinding StringFormat = "{}{0} {1}">
- <Binding Path = "Name" / >
- <Binding Path = "Surname" / >
- </ MultiBinding>
- </ TextBlock.Text>
- </ TextBlock>
- </ DataTemplate>
- </ ListBox.ItemTemplate>
- </ ListBox>
- <Button Content = "Button" HorizontalAlignment = "Left" Margin = "63,178,0,0" VerticalAlignment = "Top" Width = "75" Click = "ButtonClick" / >
- </ Grid
- </ 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.