ComboBox with multilink as the selected item?

I am trying to do the following:

Data Binding in Combobox

but with multiple links .. which means I want more than one binding ..

That way, I can display, for example, file_name as a first and last name.

How can we do this?

Thanks!

+4
source share
5 answers

Add an ItemTemplate as follows:

 <ComboBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{}{0}, {1}"> <Binding Path="LastName" /> <Binding Path="FirstName" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </ComboBox.ItemTemplate> 

( StringFormat Link )

Sidenote: In real code, I often use the Dean method , but this is the MultiBinding way of doing things.

+8
source

Do you mean something simple?

 <ComboBox> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock> <Run Text="{Binding FirstName}" /> <Run Text=" " /> <Run Text="{Binding LastName}" /> </TextBlock> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 
+4
source

You can create a binding as a MultiBinding as follows:

 <TextBlock> <TextBlock.Text> <MultiBinding Converter=" ... " ... > <Binding Path="FirstName" /> <Binding Path="LastName" /> </MultiBinding> </TextBlock.Text> </TextBlock> 
+1
source

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(); } } 
+1
source

The example given here is almost what you need.

http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

Just replace the TextBlock for the ComboBox and bind it to the SelectedItem property, not to the Text property.

0
source

All Articles