I am trying to do the following:
Data Binding in Combobox
but with multi-binding.. which means I want more than one binding..
So I can display, for example, fullname as firstn开发者_开发知识库ame + lastname.
How can we do that ?
Thanks!
Add an ItemTemplate
like this:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="LastName" />
<Binding Path="FirstName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
(StringFormat
Reference)
Sidenote: In actual code i more often use Dean's method, but this is the MultiBinding way of doing things.
Do you mean something simple like this
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding FirstName}" />
<Run Text=" " />
<Run Text="{Binding LastName}" />
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You can create a binding as MultiBinding as follows:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter=" ... " ... >
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Create MultiBinding like this:
<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 have used MainWindowViewModel as the Window's DataContext:
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 concatenates the 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();
}
}
The example given here is almost what you need.
http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx
Just swap the TextBlock for a ComboBox and bind to its SelectedItem rather than the Text property.
精彩评论