views:

670

answers:

4

Does anyone know if its possible to do a binding and (if not how to achieve the same effect) on the same property using more than one binding in sort of a template

ie A textblock that has Text bound in the expression

"{Binding Path=Contact.Title} {Binding Path=Contact.Firstname} {Binding Path=Contact.Surname}"

all in one text property

+2  A: 

AFAIK it's not possible.

This is one of the reasons to follow the MVVM pattern, create an intermediary view which reflects the data in a format that you actually want presented, so you would create a fullname property on that class that was a concatenation of those fields and then bind to that.

mattmanser
It is possible via the workaround from Andrew, but I completely agree with you. Just expose the correct property in your ViewModel (if you're following the pattern) and remove the requirement for a multi binding and any converters along the way.
Ray Booysen
+3  A: 

I had a similar issue, which led me to a blog article Colin Eberhardt wrote :

http://www.scottlogic.co.uk/blog/wpf/2009/06/silverlight-multibindings-how-to-attached-mutiple-bindings-to-a-single-property/

It works very well although I have made some adjustments to fit the specific scenario I was looking at, it was definately useful to get multibinding back.

Andrew
+1  A: 

Value Converters are one solution for binding to multiple values: http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx#11262

In that scenario you'd bind your TextBlock's Text property to the Contact object and specify a custom value converter that you've created. The converter can perform the string formatting based on property values.

James Cadd
+1  A: 

I don't think it is possible to do it directly in xaml. I would absolutely love multiple bindings to one property.

What I have learned however, is that you can accomplish things similar to this using a couple different strategies:

Using a Stackpanel:

<StackPanel Orientation="Horizontal">    
    <TextBlock Text="Hello,  "/>    
    <TextBlock Text="{Binding Contact.Title}"/>    
    <TextBlock Text="{Binding Contact.Firstname}"/> 
    <TextBlock Text="{Binding Contact.Surname}"/> 
    <TextBlock Text="!"/>
 </StackPanel>

Using a Converter:

<TextBlock Text="{Binding Contact, 
                  Converter={StaticResource ContactNameConverter}}"/>

More Info On Converters

Jeremiah