Update 29/April/2010: Have changed the examples and updated the zip file to reflect to the changes required for Silverlight 4 compatibility (see here for details).
Hopefully some of you have picked up the recent blog post by Colin Eberhardt, entitled Silverlight MultiBindings, How to attached multiple bindings to a single property. It provides an excellent basis for multi-binding support in Silverlight until they actually get on with it and become more compatible with WPF and have multi-binding directly built in!
However, when I came to use the multi-binding support, there was a limit with Colin's original design that you can only have one multi-binding per element, as you can see by this example:
<TextBlock x:Name="Block" Foreground="White" FontSize="13"
Margin="5,0,0,0">
<local:BindingUtil.MultiBinding>
<local:MultiBinding TargetProperty="Text" Converter="{StaticResource TitleConverter}">
<Binding Path="Surname"/>
<Binding Path="Forename"/>
</local:MultiBinding>
</local:BindingUtil.MultiBinding>
</TextBlock>
If you also wanted to hide the text block if forename or surname were empty, under the original design that you couldn't do this, because you can only have one target property. So I've made some changes which allows you to have multiple multi-bindings per element. So to make the example I'm describing possible the code would now look like this:
<TextBlock x:Name="Block" Foreground="White" FontSize="13"
Margin="5,0,0,0">
<local:BindingUtil.MultiBindings>
<local:MultiBindings>
<local:MultiBinding TargetProperty="Text" Converter="{StaticResource TitleConverter}">
<local:BindingCollection>
<Binding Path="Surname"/>
<Binding Path="Forename"/>
</local:BindingCollection>
</local:MultiBinding>
<local:MultiBinding TargetProperty="Visibility" Converter="{StaticResource TitleToVisibiltyConverter}">
<local:MultiBinding.Bindings>
<local:BindingCollection>
<Binding Path="Surname"/>
<Binding Path="Forename"/>
</local:BindingCollection>
</local:MultiBinding.Bindings>
</local:MultiBinding>
</local:MultiBindings>
</local:BindingUtil.MultiBindings>
</TextBlock>
So, now you can have an unlimited number of multi-bindings on the TextBlock!
Unfortunately, this makes a single multi-binding slightly more complicated because you need to declare the multi-bindings object to contain only a single binding. So the original example, if you were just having a single binding it would look like this:
<TextBlock x:Name="Block" Foreground="White" FontSize="13"
Margin="5,0,0,0">
<local:BindingUtil.MultiBindings>
<local:MultiBindings>
<local:MultiBinding TargetProperty="Text" Converter="{StaticResource TitleConverter}">
<local:BindingCollection>
<Binding Path="Surname"/>
<Binding Path="Forename"/>
</local:BindingCollection>
</local:MultiBinding>
</local:MultiBindings>
</local:BindingUtil.MultiBindings>
</TextBlock>
Source Code
you can download the source code for the project here slmultibinding.zip
WPF Compatibility
The code here should (in theory) be compatible with WPF if you want to single source your xaml code between WPF and Silverlight. Hopefully Silverlight 4 will have multi-binding and this workaround will no longer be required.
…Stefan