In this post I describe an update to the Silverlight MultiBinding solution I presented last year. This update includes support for Silverlight 4, attached properties and multiple bindings on a single object.
UPDATE: I have updated this code to include ElementName and TwoWay binding. Grab the latest copy here.
MultiBinding is a WPF feature that allows you to bind a single property to a number of sources, with the source values being combined by a value converter. This is a feature that is missing from Silverlight. About a year ago I developed a MultiBinding solution for Silverlight, which has proven very popular! I even had an email from an Microsoft Attorney asking if they could use it in the Silverlight Facebook client (How cool is that
). I was also very happy when Stefan Olson made a few updates to this code to allow multiple MultiBindings on a single object and spotted the SL4 issue. This blog post is a quick demonstration of these new features …
The following application is an example of MultiBindings in action:
In the above application, the TextBox at the top of the page is bound to both the surname and forename properties of our data-object via converter that takes the first letter of the forename and the surname. If you edit the surname or forename (hitting enter or changing focus to commit), the title is updated automatically. The title tooltip is bound to the all three object properties via a different value converter that concatenates all three.
The XAML for this binding is show below:
<TextBlock x:Name="Block" Foreground="White" FontSize="13" Margin="5,0,0,0"> <local:BindingUtil.MultiBindings> <local:MultiBindings> <local:MultiBinding TargetProperty="Text" Converter="{StaticResource TitleSummaryConverter}"> <local:MultiBinding.Bindings> <local:BindingCollection> <Binding Path="Surname"/> <Binding Path="Forename"/> </local:BindingCollection> </local:MultiBinding.Bindings> </local:MultiBinding> <local:MultiBinding TargetProperty="ToolTipService.ToolTip" Converter="{StaticResource TitleConverter}"> <local:MultiBinding.Bindings> <local:BindingCollection> <Binding Path="Surname"/> <Binding Path="Forename"/> <Binding Path="Age"/> </local:BindingCollection> </local:MultiBinding.Bindings> </local:MultiBinding> </local:MultiBindings> </local:BindingUtil.MultiBindings> </TextBlock>
In the above you can see that our TextBlock has two multibindings, one on Forename and Surname, and the other which includes all three properties. Note, the second multibinding is on the ToolTipService.ToolTip attached property.
You can download the source for Silverlight MultiBinding here: SLMultiBindingUpdate.zip – thanks again to Stefan Olson who added WPF support to this technique.
If you are interested in the technical details of how this works, I refer you to the original article which describes how the code builds a ‘virtual’ branch on the visual tree in order to evaluate your bindings:
I hope this update is of use to SL4 developers. If you have any feedback, please leave a comment below.
You can download the source for Silverlight MultiBinding here: SLMultiBindingUpdate.zip
Regards, Colin E.
Tags: binding, multibinding, silverlight





@SilverlightDevelopper, @Jonathan Perl,
TwoWay binding and ElementName … here you go:
http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/
Hey again Collin !
Any news for that TwoWay MultiBinding ?? ^^
Colin you are the man!
Hello Collin,
One again thank you for this amazing functionality that Silverlight misses a lot.
I have some problems using your solution though because I need a TwoWay binding (Silverlight4), let me explain myself :
I have a product wich has an inventory status. To be compliant with an older software, I need to display this number using a ratio stocked into the said product.
So I have a product which has two property :
product.InventoryStatus (int)
product.Ratio (double)
And, to display it, I use a numericUpDown :
And this is my converter :
public class DecimalStockConverter : IMultiValueConverter
{
private double _ratio = 1;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double val = System.Convert.ToDouble(values[0] ?? 0);
double ratio = System.Convert.ToDouble(values[1] ?? 0);
while (val > 1)
val /= 10;
_ratio = ratio;
return (val * ratio);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
double val= (_ratio == 0 ? 0 : System.Convert.ToDouble(value ?? 0) / _ratio);
while (val < 1)
val *= 10;
int result = System.Convert.ToInt32(Math.Round(val, 0));
return (new object[] { result, _ratio });
}
}
The problem is : When I change the value of the numericUpDown using the UI, the binding seems to break and I never enter the ConvertBack method. So, does your solution allows a TwoWay MultiBinding ?
If it does, could you please give a sample with an implementation of the convertBack method, I'll may be able to find out what I do wrong !
Thank you !!
@SilverlightDevelopper,
To be honest I have not tried TwoWay binding! I will give it a go and see if a can find / fix this issue.
Colin E.
Great solution!
It is possible to use ElementName with it?
Re: ElementName binding, good idea … I will add that shortly.
(Also thanks for you help for my previous problem, that fixed the issue)
Is there any way to update the code to have ElementName work?
I did a little research in your source code to try and implement it myself but I got pretty lost with the piggy backing and attached properties.
If at all possible it would be extremely helpful!! Thank you.
Does not work for TargetProperty=”datagrid:DataGrid.Columns”.
@Jonathan,
This is because you are binding to an attached property with a namespace prefix. There is no simple way for me to resolve these namespace prefixes, so I make the assumption that attached properties are always in the
System.Windows.Controlsnamespace. You could edit the code at the point where the above namespace is referenced in order to look in the namespace that contains the DataGrid.Hope that helps!
Regards, Colin E.
Hi Colin,
Thanks for sharing – Can this solution do this:
I want to do a String Format (from a resource localisation file) and replace the values like this (this code works in SL4 for a single bind value)
Is it possible to output something similar with your solution, but with multiple bindings, like:
I downloaded and tried it but StringFormat was not recognised.
Thanks for any guidance…
@Rodney,
Sorry – looks like your markup was stripped in the comment. I would like to help, but cannot see your code!
Colin E.
Thanks for the code, I’m suprised it wasn’t included from the start, but I’m having a slight issue with using it with collections.
I’ve created a MultiBinding against the ItemsSource property on a ListBox. I’ve then bound it with two Bindings, one is a collection of entities and the other is an Id field. I then have a converter which filters the collection using the id field.
I have added an item to the collection, however the binding isn’t being updated. I imagine it is potentially an issue with the MultiBinding not checking INotifyCollectionChanged as well as INotifyPropertyChanged? As a workaround I have put MyListBox.GetBindingExpression(ListBox.ItemsSourceProperty).UpdateSource() however this is not causing the breakpoint in my converter to be hit.
Can you advise if this is correct or where I’m going wrong? I’m currently using SL3, however I can’t see that this should have an impact in this area.
Thanks,
Duncan
@Duncan,
The ItemsSource property is a special property which understand the INotifyCollectionChanged interface and responds to its events. The databinding framework is geared towards binding of single properties and responding to PropertyChanged events, so I can see why this would not work. I think the best solution is to create a ViewModel which exposes your filtered collection then bind this to your view.,
Regards, Colin E.
[...] Silverlight MultiBinding solution for Silverlight 4 (Colin Eberhardt) [...]
[...] Silverlight MultiBinding solution for Silverlight 4 (Colin Eberhardt) [...]
Silverlight MultiBinding solution for Silverlight 4 | Colin Eberhardt’s Adventures in WPF…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] Silverlight MultiBinding solution for Silverlight 4 [...]