Create a MultiBinding as follows:
<Window x:Class="WpfTestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfTestApp="clr-namespace:WpfTestApp" Title="MainWindow" Height="350" Width="525" > <Window.Resources> <WpfTestApp:ConcatenateStringsConverter x:Key="_concatenateStringsConverter" /> </Window.Resources> <Grid x:Name="LayoutRoot" Style="{StaticResource RectangleHighlighter}"> <ComboBox Width="200" Height="40"> <ComboBox.Items> <ComboBoxItem > <ComboBoxItem.Content> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource _concatenateStringsConverter}"> <Binding Mode="OneWay" Path="FirstName" /> <Binding Mode="OneWay" Path="LastName" /> </MultiBinding> </TextBlock.Text> </TextBlock> </ComboBoxItem.Content> </ComboBoxItem> </ComboBox.Items> </ComboBox> </Grid> </Window>
I used MainWindowViewModel as a WindowContext Window:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); } } public class MainWindowViewModel :INotifyPropertyChanged { public MainWindowViewModel() { FirstName = "Souvik"; LastName = "Basu"; } private string _firstName; public string FirstName { get { return _firstName; } set { if (_firstName != value) { _firstName = value; OnPropertyChange("FirstName"); } } } private string _lastName; public string LastName { get { return _lastName; } set { if (_lastName != value) { _lastName = value; OnPropertyChange("LastName"); } } } protected void OnPropertyChange(string propertyName) { if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
The converter combines multiple binding values.
class ConcatenateStringsConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return values[0].ToString() + " " + values[1].ToString(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
source share