Improvements to Silverlight Multi-binding support

by StefanOlson 19. July 2009 14:57

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}">
                <Binding Path="Surname"/>                            
                <Binding Path="Forename"/>
            </local:MultiBinding>
            <local:MultiBinding TargetProperty="Visibility" Converter="{StaticResource TitleToVisibiltyConverter}">
                <Binding Path="Surname"/>                            
                <Binding Path="Forename"/>
            </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}">
                <Binding Path="Surname"/>                            
                <Binding Path="Forename"/>
            </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

Tags:

Silverlight

Comments

7/19/2009 6:28:27 PM #

Hi Stefan,

Nice one - good improvement. Did you also see the little amendment I added at the bottom of the page to allow binding of attached properties:

www.scottlogic.co.uk/.../#comment-2124

Regards,
Colin E.

Colin E.

7/19/2009 7:16:00 PM #

Colin,

Thanks so much for the original article, the lack of multi-binding is extremely frustrating when working in Silverlight.

Yes, I did include the change you made to support attached properties into the code I've used.  I'll be interested to see if anyone needs to use it for properties outside of the System.Windows.Controls that you have referenced. It would be nice to avoid having to hardcode that.

...Stefan

StefanOlson

7/19/2009 11:52:09 PM #

Cool - glad you included that.

Yes, I did wonder about hard-coding the System.Windows.Controls namespace, however it was a compromise (these things always are). To do it properly I think you would have to allow namespace prefixes - I have no idea if the XAML parser exposes the namespace prefix mappings for a document. So, the compromise was just to keep it simple!

Colin E.

Colin E.

2/18/2010 7:15:36 PM #

Silverlight multi-binding for WPF

Silverlight multi-binding for WPF

Stefan Olson's Blog

Add comment




biuquote
  • Comment
  • Preview
Loading



About the author

Stefan Olson is the Managing Director of Olson Software.  He has been developing software using Microsoft Technologies for nearly 20 years.

He is currently working on building the next generation Virtual Tour software in WPF and Silverlight for www.palacevirtualtours.com.

Tag cloud