views:

15

answers:

2

How do bind to more than one property in WPF?
I know that you can use the ItemStringFormat="{0} my hard coded string" to describe a string value but I am interested in something like this ItemStringFormat="{0} ({1})" where item zero is a property and item one is a property in the bound class.

public class ExchangeRate
{
    public int ID { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }
   public decimal Rate { get; set; }
}

<ComboBox Margin="5,0" Name="Currency" ItemsSource="{Binding}" DisplayMemberPath="Description" SelectedValuePath="Code"/>

This will give me a list with all the currency descriptions but what i want is something like this

"US Dollar (USD)"

where is "US Dollar" is the property Description and "USD" is the prorpety Code

A: 

You can use a MultiBinding with an IMultiValueConverter to convert from multiple sources into a single target property (ie: Text).

Reed Copsey
A: 

Yes, you are correct and for the sake of documentation i will provide that solution here using the example above.

<ComboBox Margin="5,0" Name="CurrentCurrency" ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} ({1})">
<Binding Path="Description" />
<Binding Path="Code" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>