MultiBinding and IMultiValueConverter in Silverlight?

431 views
Skip to first unread message

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 14, 2009, 3:59:54 PM2/14/09
to wpf-di...@googlegroups.com

Hey guys,

 

I am thinking of taking a shot at rewriting MultiBinding for Silverlight (it’s not currently available). Before I dive into Reflector, did any one of you guys already do this?

 

Cheers,

Laurent

--

Laurent Bugnion [Microsoft MVP, MCP]

Software engineering, Blog: http://www.galasoft.ch

PhotoAlbum: http://www.galasoft.ch/pictures

Support children in Calcutta: http://www.calcutta-espoir.ch

 

Corrado Cavalli

unread,
Feb 15, 2009, 2:12:55 AM2/15/09
to wpf-di...@googlegroups.com

Just wondering…

In a M-V-VM world, do we really need MultiBinding?

 

-Corrado

Dr. WPF

unread,
Feb 15, 2009, 3:57:54 AM2/15/09
to wpf-di...@googlegroups.com

I'm as big a proponent of MVVM as anyone, but there are some things I can do in a view using a multibinding that I just wouldn't want to give up.  Very often, these are purely view specific tasks (e.g., numbering items presented in an ItemsControl) where I do not want to impose any extra requirements on the VM.

Quoting "wpf-di...@googlegroups.com" <wpf-di...@googlegroups.com>:

> Just wondering.


>
> In a M-V-VM world, do we really need MultiBinding?
>
>
>
> -Corrado
>
>
>
> From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
> On Behalf Of Laurent Bugnion, GalaSoft [MVP, MCP]
> Sent: sabato 14 febbraio 2009 22:00
> To: wpf-di...@googlegroups.com
> Subject: [WPF Disciples] MultiBinding and IMultiValueConverter in
> Silverlight?
>
>
>
> Hey guys,
>
>
>
> I am thinking of taking a shot at rewriting MultiBinding for Silverlight
> (it's not currently available). Before I dive into Reflector, did any one of
> you guys already do this?
>
>
>
> Cheers,
>
> Laurent
>
> --
>
> Laurent Bugnion [Microsoft MVP, MCP]
>

> Software engineering, Blog:  <http://www.galasoft.ch/>

> Support children in Calcutta:  <http://www.calcutta-espoir.ch/>
> http://www.calcutta-espoir.ch
>
>
>
>
>

Dr. WPF - Online Office at http://www.drwpf.com/blog/


Corrado Cavalli

unread,
Feb 15, 2009, 4:16:34 AM2/15/09
to wpf-di...@googlegroups.com

Makes sense (as usual… J)

but if I should implement it myself I’d stick with VM implementation, if its inside the VM, it’s easier to test.

 

-Corrado

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Dr. WPF


Sent: domenica 15 febbraio 2009 09:58
To: wpf-di...@googlegroups.com

Subject: [WPF Disciples] Re: MultiBinding and IMultiValueConverter inSilverlight?

 

I'm as big a proponent of MVVM as anyone, but there are some things I can do in a view using a multibinding that I just wouldn't want to give up.  Very often, these are purely view specific tasks (e.g., numbering items presented in an ItemsControl) where I do not want to impose any extra requirements on the VM.

> In a M-V-VM world, do we really need MultiBinding?
>
>
>
> -Corrado
>
>
>
> From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
> On Behalf Of Laurent Bugnion, GalaSoft [MVP, MCP]
> Sent: sabato 14 febbraio 2009 22:00
> To: wpf-di...@googlegroups.com
> Subject: [WPF Disciples] MultiBinding and IMultiValueConverter in
> Silverlight?
>
>
>
> Hey guys,
>
>
>
> I am thinking of taking a shot at rewriting MultiBinding for Silverlight
> (it's not currently available). Before I dive into Reflector, did any one of
> you guys already do this?
>
>
>
> Cheers,
>
> Laurent
>
> --
>
> Laurent Bugnion [Microsoft MVP, MCP]
>

> Software engineering, Blog:  <http://www.galasoft.ch/>

> Support children in Calcutta:  <http://www.calcutta-espoir.ch/>
> http://www.calcutta-espoir.ch
>
>
>
>
>

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 15, 2009, 4:23:33 AM2/15/09
to wpf-di...@googlegroups.com

Hey Corrado,

 

I rarely use MultiBinding, but there is one case where I find them really useful: When I need to bring an instance of a UI Element in the code. The real life example I have in mind is this (in fact it’s funny we talk about it, because I have a blog post half written about this. Credits due to the Doc, who showed me this one day in 2007…)

 

The problem is when you want to apply a resource to an element based on a value. For example, you want to apply a Brush to a Border in a ListBox, depending on the value of a Customer account. If the account is in the 0 – 1000 range, choose a red-ish brush, 1001-2000 orange-ish brush, 2001-3000 Green-ish brush, etc…

 

I can do the conversion purely in the VM, I have the problem of finding the resource. Usually, the best way (from an integrator’s perspective) is to use TryFindResource, because it allows me to move the resources without altering the code. I am a big fan of TryFindResource, because it allows easy resource refactoring. I cannot count the times where I had to move a resource from a UserControl (for example) into a ResourceDictionary.

 

So now I am left with a problem: How do I access the UIElement on which I want to call TryFindResource. If I do this in the VM, I need a reference to a View element in the VM, and I try to avoid this if possible. The alternative is using the MultiBinding and a IMultiValuerConverter. If the first binding in the MultiBinding is set to the UIElement itself (using RelativeSource Self), then I can call TryFindResource on it based on the second value in the MultiBinding (for example the Account number).

 

How would you handle the cases (and there are some others than mentioned here) where you need a reference to a View element in the VM?

 

Thanks,

Laurent

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.0.233 / Virus Database: 270.10.23/1952 - Release Date: 02/13/09 18:29:00

Corrado Cavalli

unread,
Feb 15, 2009, 5:30:31 AM2/15/09
to wpf-di...@googlegroups.com

Hey Laurent,

What’s wrong with this?

 

  <Grid>

    <Grid.Resources>

      <SolidColorBrush x:Key="YellowBrush" Color="Yellow" />

      <SolidColorBrush x:Key="RedBrush" Color="Red" />

      <SolidColorBrush x:Key="BlueBrush" Color="Blue" />

    </Grid.Resources>

   

    <Border BorderThickness="2">

      <Border.Style>

        <Style TargetType="{x:Type Border}">

          <Style.Triggers>

            <DataTrigger Binding="{Binding Path=CustomerThreshold}" Value="0">

              <Setter Property="BorderBrush" Value="{StaticResource YellowBrush}" />

            </DataTrigger>

            <DataTrigger Binding="{Binding Path=CustomerThreshold}" Value="1">

              <Setter Property="BorderBrush" Value="{StaticResource RedBrush}" />

            </DataTrigger>

            <DataTrigger Binding="{Binding Path=CustomerThreshold}" Value="2">

              <Setter Property="BorderBrush" Value="{StaticResource BlueBrush}" />

            </DataTrigger>

          </Style.Triggers>

        </Style>

      </Border.Style>    

    <ListBox />

    </Border>   

  </Grid>

 

Of course CustomerThreshold is a property of my ViewModel that drives border color

 

>How would you handle the cases (and there are some others than mentioned here) where you need a reference to a View element in the VM?

In case I need it I’d probably pass a reference to the view on ViewModel constructor.

 

-Corrado

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion, GalaSoft [MVP, MCP]


Sent: domenica 15 febbraio 2009 10:24
To: wpf-di...@googlegroups.com

Subject: [WPF Disciples] Re: MultiBinding and IMultiValueConverter in Silverlight?

 

Hey Corrado,

 

I rarely use MultiBinding, but there is one case where I find them really useful: When I need to bring an instance of a UI Element in the code. The real life example I have in mind is this (in fact it’s funny we talk about it, because I have a blog post half written about this. Credits due to the Doc, who showed me this one day in 2007…)

 

The problem is when you want to apply a resource to an element based on a value. For example, you want to apply a Brush to a Border in a ListBox, depending on the value of a Customer account. If the account is in the 0 – 1000 range, choose a red-ish brush, 1001-2000 orange-ish brush, 2001-3000 Green-ish brush, etc…

 

I can do the conversion purely in the VM, I have the problem of finding the resource. Usually, the best way (from an integrator’s perspective) is to use TryFindResource, because it allows me to move the resources without altering the code. I am a big fan of TryFindResource, because it allows easy resource refactoring. I cannot count the times where I had to move a resource from a UserControl (for example) into a ResourceDictionary.

 

So now I am left with a problem: How do I access the UIElement on which I want to call TryFindResource. If I do this in the VM, I need a reference to a View element in the VM, and I try to avoid this if possible. The alternative is using the MultiBinding and a IMultiValuerConverter. If the first binding in the MultiBinding is set to the UIElement itself (using RelativeSource Self), then I can call TryFindResource on it based on the second value in the MultiBinding (for example the Account number).

 

How would you handle the cases (and there are some others than mentioned here) where you need a reference to a View element in the VM?

 

Thanks,

Laurent

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Corrado Cavalli


Sent: Sunday, February 15, 2009 8:13 AM

No virus found in this incoming message.

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 15, 2009, 7:21:06 AM2/15/09
to wpf-di...@googlegroups.com
Hey Corrado,

That could actually work. Let me check it in my sample application and
ponder about this. Man I am not sure how people were ever solving
programming problems before the internet...

That leaves us with the issue that it doesn't work in Silverlight, but
neither does my MultiBinding solution. Starting to wonder if I can do
something with VSM to solve that issue in Silverlight...

Thanks,
Laurent

Hey Laurent,

<Grid>

<Grid.Resources>

</Grid.Resources>

<Border BorderThickness="2">

<Border.Style>

<Style.Triggers>

</DataTrigger>

</DataTrigger>

</DataTrigger>

</Style.Triggers>

</Style>

</Border.Style>

<ListBox />

</Border>

</Grid>

-Corrado

Hey Corrado,

Credits due to the Doc, who showed me this one day in 2007.)

The problem is when you want to apply a resource to an element based on
a value. For example, you want to apply a Brush to a Border in a
ListBox, depending on the value of a Customer account. If the account is

in the 0 - 1000 range, choose a red-ish brush, 1001-2000 orange-ish
brush, 2001-3000 Green-ish brush, etc.

Thanks,

Laurent

Just wondering.

In a M-V-VM world, do we really need MultiBinding?

-Corrado

From: wpf-di...@googlegroups.com
[mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion,
GalaSoft [MVP, MCP]
Sent: sabato 14 febbraio 2009 22:00
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] MultiBinding and IMultiValueConverter in
Silverlight?

Hey guys,

I am thinking of taking a shot at rewriting MultiBinding for Silverlight
(it's not currently available). Before I dive into Reflector, did any
one of you guys already do this?

Cheers,

Laurent

--

Laurent Bugnion [Microsoft MVP, MCP]

Software engineering, Blog: http://www.galasoft.ch

<http://www.galasoft.ch/>

PhotoAlbum: http://www.galasoft.ch/pictures
<http://www.galasoft.ch/pictures>

Support children in Calcutta: http://www.calcutta-espoir.ch

<http://www.calcutta-espoir.ch/>

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.0.233 / Virus Database: 270.10.23/1952 - Release Date:
02/13/09 18:29:00


No virus found in this incoming message.
Checked by AVG - www.avg.com

Version: 8.0.237 / Virus Database: 270.10.23/1953 - Release Date: 02/14/09
18:01:00

Corrado Cavalli

unread,
Feb 15, 2009, 8:40:59 AM2/15/09
to wpf-di...@googlegroups.com
Please give us Triggers in SL 3.0! :-D
Man I miss them a lot, VSM, as Brennon pointed out in another thread, are
good for modifications but not in case you need to add new states
(practically simulating new Trigger Addition)

-Corrado

-----Original Message-----
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
On Behalf Of Laurent Bugnion, GalaSoft [MVP, MCP]
Sent: domenica 15 febbraio 2009 13:21
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: MultiBinding and IMultiValueConverter in
Silverlight?


Hey Corrado,

That could actually work. Let me check it in my sample application and


ponder about this. Man I am not sure how people were ever solving
programming problems before the internet...

That leaves us with the issue that it doesn't work in Silverlight, but
neither does my MultiBinding solution. Starting to wonder if I can do
something with VSM to solve that issue in Silverlight...

Thanks,
Laurent

Hey Laurent,

<Grid>

<Grid.Resources>

</Grid.Resources>

<Border BorderThickness="2">

<Border.Style>

<Style.Triggers>

</DataTrigger>

</DataTrigger>

</DataTrigger>

</Style.Triggers>

</Style>

</Border.Style>

<ListBox />

</Border>

</Grid>

-Corrado

Hey Corrado,

Credits due to the Doc, who showed me this one day in 2007.)

The problem is when you want to apply a resource to an element based on
a value. For example, you want to apply a Brush to a Border in a
ListBox, depending on the value of a Customer account. If the account is

in the 0 - 1000 range, choose a red-ish brush, 1001-2000 orange-ish
brush, 2001-3000 Green-ish brush, etc.

Thanks,

Laurent

Just wondering.

In a M-V-VM world, do we really need MultiBinding?

-Corrado

From: wpf-di...@googlegroups.com
[mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion,
GalaSoft [MVP, MCP]
Sent: sabato 14 febbraio 2009 22:00
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] MultiBinding and IMultiValueConverter in
Silverlight?

Hey guys,

I am thinking of taking a shot at rewriting MultiBinding for Silverlight
(it's not currently available). Before I dive into Reflector, did any
one of you guys already do this?

Cheers,

Laurent

--

Laurent Bugnion [Microsoft MVP, MCP]

Software engineering, Blog: http://www.galasoft.ch

<http://www.galasoft.ch/>

PhotoAlbum: http://www.galasoft.ch/pictures
<http://www.galasoft.ch/pictures>

Support children in Calcutta: http://www.calcutta-espoir.ch

<http://www.calcutta-espoir.ch/>

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.0.233 / Virus Database: 270.10.23/1952 - Release Date:
02/13/09 18:29:00


No virus found in this incoming message.
Checked by AVG - www.avg.com

Marlon Grech

unread,
Feb 15, 2009, 8:55:59 AM2/15/09
to wpf-di...@googlegroups.com
SL without Triggers for me is really a pain.... I used SL 2.0 a bit but there are somethings that just make me wanna bang my head to the wall..... In actual fact I think I prefer going XBAP (whenever I can)

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/

Eric Burke

unread,
Feb 15, 2009, 9:03:43 AM2/15/09
to wpf-di...@googlegroups.com
We use this technique heavily in Yahoo! Messenger.  Most often it's like this case where you pass in the item you're going to operate on, and then other value(s) that might have to participate in the operation.


From: "Laurent Bugnion, GalaSoft [MVP, MCP]" <lau...@galasoft.ch>
To: wpf-di...@googlegroups.com
Sent: Sunday, February 15, 2009 4:23:33 AM

Corrado Cavalli

unread,
Feb 15, 2009, 9:19:51 AM2/15/09
to wpf-di...@googlegroups.com

Sure, a few months ago someone asked me how to change textbox background based on two different variables and MultiBinding did the trick! J

http://blogs.ugidotnet.org/corrado/archive/2008/07/11/93368.aspx

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Eric Burke
Sent: domenica 15 febbraio 2009 15:04
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: MultiBinding and IMultiValueConverter in Silverlight?

 

We use this technique heavily in Yahoo! Messenger.  Most often it's like this case where you pass in the item you're going to operate on, and then other value(s) that might have to participate in the operation.

 


Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 15, 2009, 11:55:39 AM2/15/09
to wpf-di...@googlegroups.com

I am busy writing a comparison of MultiBinding vs DataTrigger. Can’t say I really can favor one or the other in the moment. Both have interesting features. More to follow ;)

Pavan Podila

unread,
Feb 15, 2009, 12:46:32 PM2/15/09
to wpf-di...@googlegroups.com
I use MultiBinding quite heavily. In fact the PyBinding extension that I built some time back uses MultiBinding extensively. PyBinding is a MarkupExtension that allows you to use IronPython for scripting simple expressions and even complex ones inside of Xaml. It is a great way to provide pseudo-properties on the VM, without really declaring the properties on the VM !
 
I'll blog about this pattern once I get some free time. I am using this pseudo-property technique in my current project and has worked very well so far.
 
- Pavan

--
Pavan Podila
http://blog.pixelingene.com
------------------------------------------

Jaime Rodriguez

unread,
Feb 16, 2009, 2:41:40 AM2/16/09
to wpf-di...@googlegroups.com

Laurent,

 
Can you share a concise sample where you have used this technique and felt there was no workaround??   (off-line is fine if you can’t share publicly).
If feels that you have UI that could have been driven with viewmodel. Rather then you pushing values to the UIElements, the UIElements  should be picking state from the ViewModel and then locating the resources (via triggers?) based on these values?    I am guessing other scenarios could be more complicated?

 

Corrado, I think you have a point that Multibinding usage is less when you have a viewmodel, but I think there are still scenarios where Multibinding is warranted.   I use Multibinding seldomly, but do use it. from looking at a few apps, I think a recurrent scenario is that I do not like to replicate properties in the view model for no reason.   Simplest one is First + LastName..  In ViewModel, these are separate because I need them that way, but sometimes the view wants to display it together ..  I could add an extra property in VM to hold FullName, but then I end up with say 3 variations of this ( middlename, middle initial, etc.. )  so I let MultiBinding do its magic and create a reusable MultiValue converter to do the work.. The logic is still in the converter (not so much in the view).   Keeping atomicity on the data is important to me (seems to bite me often)..     Pavan’s scenario seems valid and interesting too..  I am sure there are a few others …

 

Cheers,

Corrado Cavalli

unread,
Feb 16, 2009, 3:52:52 AM2/16/09
to wpf-di...@googlegroups.com
Hi Jamie,
I think that in a M-V-VM app, the use or Multibinding vs Viewmodel
is finally a matter of personal taste.
Since Blend does not support Multibinding (Brennon jump in if I'm
wrong...) and since i like to stay away from xaml as much as i
can, on my projects I'd probably add a FullName property to the
ViewModel, that has also the plus of being a more testable
solution.

About your SouthRidge demo: Do you have an idea why if i load it
in VS2008 and double click main view xaml file, my VS2008 closes
without any message? (Blends opens it fine...)

-Corrado

On Mon Feb 16 01:41:40 CST 2009, Jaime Rodriguez
<jai...@microsoft.com> wrote:

> Laurent,
>
> Can you share a concise sample where you have used this technique
> and felt there was no workaround?? (off-line is fine if you

> can?t share publicly).


> If feels that you have UI that could have been driven with
> viewmodel. Rather then you pushing values to the UIElements, the
> UIElements should be picking state from the ViewModel and then
> locating the resources (via triggers?) based on these values?
> I am guessing other scenarios could be more complicated?
>
> Corrado, I think you have a point that Multibinding usage is less
> when you have a viewmodel, but I think there are still scenarios
> where Multibinding is warranted. I use Multibinding seldomly,
> but do use it. from looking at a few apps, I think a recurrent
> scenario is that I do not like to replicate properties in the
> view model for no reason. Simplest one is First + LastName..
> In ViewModel, these are separate because I need them that way,
> but sometimes the view wants to display it together .. I could
> add an extra property in VM to hold FullName, but then I end up
> with say 3 variations of this ( middlename, middle initial, etc..
> ) so I let MultiBinding do its magic and create a reusable
> MultiValue converter to do the work.. The logic is still in the
> converter (not so much in the view). Keeping atomicity on the

> data is important to me (seems to bite me often).. Pavan?s

> scenario seems valid and interesting too.. I am sure there are a

> few others ?


>
> Cheers,
>
>
>
>
> From: wpf-di...@googlegroups.com
> [mailto:wpf-di...@googlegroups.com] On Behalf Of Eric Burke
> Sent: Sunday, February 15, 2009 6:04 AM
> To: wpf-di...@googlegroups.com
> Subject: [WPF Disciples] Re: MultiBinding and
> IMultiValueConverter in Silverlight?
>
> We use this technique heavily in Yahoo! Messenger. Most often
> it's like this case where you pass in the item you're going to
> operate on, and then other value(s) that might have to
> participate in the operation.
>
> ________________________________
> From: "Laurent Bugnion, GalaSoft [MVP, MCP]"
> <lau...@galasoft.ch>
> To: wpf-di...@googlegroups.com
> Sent: Sunday, February 15, 2009 4:23:33 AM
> Subject: [WPF Disciples] Re: MultiBinding and
> IMultiValueConverter in Silverlight?
> Hey Corrado,
>
> I rarely use MultiBinding, but there is one case where I find
> them really useful: When I need to bring an instance of a UI
> Element in the code. The real life example I have in mind is this

> (in fact it?s funny we talk about it, because I have a blog post

> half written about this. Credits due to the Doc, who showed me

> this one day in 2007?)


>
> The problem is when you want to apply a resource to an element
> based on a value. For example, you want to apply a Brush to a
> Border in a ListBox, depending on the value of a Customer

> account. If the account is in the 0 ? 1000 range, choose a

> red-ish brush, 1001-2000 orange-ish brush, 2001-3000 Green-ish

> brush, etc?


>
> I can do the conversion purely in the VM, I have the problem of

> finding the resource. Usually, the best way (from an integrator?s

> Just wondering?


> In a M-V-VM world, do we really need MultiBinding?
>
> -Corrado
>
> From: wpf-di...@googlegroups.com
> [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent
> Bugnion, GalaSoft [MVP, MCP]
> Sent: sabato 14 febbraio 2009 22:00
> To: wpf-di...@googlegroups.com
> Subject: [WPF Disciples] MultiBinding and IMultiValueConverter in
> Silverlight?
>
> Hey guys,
>
> I am thinking of taking a shot at rewriting MultiBinding for

> Silverlight (it?s not currently available). Before I dive into

> Reflector, did any one of you guys already do this?
>
> Cheers,
> Laurent
> --
> Laurent Bugnion [Microsoft MVP, MCP]
> Software engineering, Blog:

> http://www.galasoft.ch<http://www.galasoft.ch/>


> PhotoAlbum: http://www.galasoft.ch/pictures
> Support children in Calcutta:

> http://www.calcutta-espoir.ch<http://www.calcutta-espoir.ch/>


>
>
> No virus found in this incoming message.

> Checked by AVG - www.avg.com<http://www.avg.com/>

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 16, 2009, 4:00:55 AM2/16/09
to wpf-di...@googlegroups.com

Hi Jaime,

 

Before I start, I sent you an email regarding MIX, did you get it?

 

As I replied to Corrado, I think that the DataTrigger solution he proposes can solve that problem. I have written a sample application using both techniques and trying to compare advantages and inconveniencies of both. I intend to blog that right after I publish part two of my IsSynchronizedWithCurrentItem post.

 

My first impression is that both solutions are non-perfect. The DataTrigger solution is more verbose (since it is mostly implemented in XAML), which is annoying especially if you use the same logic in multiple place. Also, if you need to add values (to an enum, for example) or ranges (in the Threshold model we were discussing), you need to modify multiple places in the XAML, while in the best case you don’t need to modify the Converter at all (and in the worst case, your changes will be localized to one class only). With the Converter, you simply reuse the same class in multiple places, which reduces the amount of code that needs to be maintained. However, as you mention, the Converter solution is a hybrid View/ViewModel, and is less “clean”.

 

I also compared the testability of both solutions. Unit testing a IMultiValueConverter is possible, but is a bit awkward because you start unit testing view logic, which is always a bit weird and which I try to avoid. Unit testing the ViewModel is easy and well known, no surprises there (but it doesn’t test the view logic at all, and you cannot verify that the resource will indeed be found. This is typical of WPF where bindings written in XAML cannot be unit tested, but I never saw this being an issue in the real world).

 

If we want to push to the extreme, the Converter solution answers better to the SOLID principles (the Converter has one purpose only, which is to convert, and thus has less reasons to change than a multi purpose ViewModel class ;) Thankfully I am pragmatic and will not judge the solutions based on that. After all, we can argue that keeping the ViewModel logic in the ViewModel makes more sense than starting to involve converters.

 

About your comment that I am “pushing” the values to the view, I don’t feel this way. After all, the MultiBinding is in XAML, same as the DataTrigger, so the action is started by the View in both cases. As I mentioned, the Converter in this case is a hybrid View/ViewModel, which is less clean, but I can live with that.

 

In short: It’s a bit hard to decide for one solution or the other. I will probably recommend the DataTrigger solution in my blog post, due to the fact that it’s more in line with the ViewModel pattern.

 

And as a conclusion, none of these solutions work in Silverlight…

 

Cheers,

Laurent

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 16, 2009, 4:16:19 AM2/16/09
to wpf-di...@googlegroups.com
I agree with Corrado (no surprises there ;))

Blend also doesn't support data triggers, so we are forced to work in XAML
editor anyway here... or did I miss something?

Cheers,
Laurent

-Corrado

Version: 8.0.237 / Virus Database: 270.10.23/1953 - Release Date: 02/15/09
18:09:00

Corrado Cavalli

unread,
Feb 16, 2009, 4:40:33 AM2/16/09
to wpf-di...@googlegroups.com
You're right, that's another of the (many) things I'd love to have
in Blend to increase productivity.

-Corrado


On Mon Feb 16 03:16:19 CST 2009, "Laurent Bugnion, GalaSoft [MVP,

Eric Burke

unread,
Feb 16, 2009, 9:49:23 AM2/16/09
to wpf-di...@googlegroups.com
The other reason MultiBinding helps in scenarios like this is because of localization.  If you have this:
 
<TextBlock>
    <Run Text="Hello, "/>
    <TextBlock Text="{Binding FirstName}"/>
    <Run Text=", how are you?"/>
</TextBlock>
 
When you run locbaml it's much more difficult for translators to figure out what should happen, especially if they don't have any knowledge of the app (e.g, a shop that just translates words and gets paid per word or phrase).
 
If you use a MultiBinding you can pass a format string and the args and it becomes much easier to translate because you can note to the translators in the LocBaml comments that {0} is FirstName, etc., and as long as they are trained to keep the {0} notation, it will work just fine.  If you use a Converter rather than a FormatString, you can use it to look up localized information and do the correct thing with the values.  This allows you to decouple the localization process from your logic, which helps the translators out and limits the back-and-forth (which just costs you money and time).
 
<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="..." OR FormatString="...">
            <Binding Path="FirstName"/>
            ...
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

 


From: Jaime Rodriguez <jai...@microsoft.com>
To: "wpf-di...@googlegroups.com" <wpf-di...@googlegroups.com>
Sent: Monday, February 16, 2009 2:41:40 AM

Josh Smith

unread,
Feb 16, 2009, 9:57:43 AM2/16/09
to wpf-di...@googlegroups.com
That's an interesting point, Eric.  Thanks for sharing that one.  Globalization is a very complex subject, once you really get into it.

Eric Burke

unread,
Feb 16, 2009, 10:09:22 AM2/16/09
to wpf-di...@googlegroups.com
No problem.  We ran into a number of issues with localization on Messenger.  Another major issue was trying to make locbaml work well with our legacy organizational translation database.  Our localization expert spent a lot of time writing scripts to make it all seamless.
 
If anyone is running into issues with localization, feel free to ping me offline.  We translated our large app into DE and TW languages, so we've seen a lot. ;)


From: Josh Smith <flappl...@gmail.com>
To: wpf-di...@googlegroups.com
Sent: Monday, February 16, 2009 9:57:43 AM

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Feb 16, 2009, 10:20:06 AM2/16/09
to wpf-di...@googlegroups.com

Since we are on the topic of localization, one of the swiss WPF developers I know came up with what I think is a good solution

 

http://www.wpftutorial.net/Localisation.html

 

Cheers,

Laurent

Version: 8.0.237 / Virus Database: 270.10.24/1954 - Release Date: 02/15/09 18:09:00

Eric Burke

unread,
Feb 16, 2009, 10:27:22 AM2/16/09
to wpf-di...@googlegroups.com
Ha, this is exactly what we do too! ;)

 

From: "Laurent Bugnion, GalaSoft [MVP, MCP]" <lau...@galasoft.ch>
To: wpf-di...@googlegroups.com
Sent: Monday, February 16, 2009 10:20:06 AM

Josh Smith

unread,
Feb 16, 2009, 10:31:48 AM2/16/09
to wpf-di...@googlegroups.com
I especially like his trick that maps into the standard default xml namespace.  Very clever!  :)

Corrado Cavalli

unread,
Feb 16, 2009, 10:45:03 AM2/16/09
to wpf-di...@googlegroups.com
Yeah, very clever, I'll probably use it for my custom
behaviors...

-Corrado

On Mon Feb 16 09:31:48 CST 2009, Josh Smith
<flappl...@gmail.com> wrote:

> I especially like his trick that maps into the standard default
> xml
> namespace. Very clever! :)
>
> On Mon, Feb 16, 2009 at 10:20 AM, Laurent Bugnion, GalaSoft [MVP,
> MCP]
> <lau...@galasoft.ch> wrote:
>
>> Since we are on the topic of localization, one of the swiss WPF
>> developers I know came up with what I think is a good solution
>>
>>
>>
>> http://www.wpftutorial.net/Localisation.html
>>
>>
>>
>> Cheers,
>>
>> Laurent
>>
>>
>>

>> *From:* wpf-di...@googlegroups.com [mailto:
>> wpf-di...@googlegroups.com] *On Behalf Of *Josh Smith
>> *Sent:* Monday, February 16, 2009 3:58 PM
>>
>> *To:* wpf-di...@googlegroups.com
>> *Subject:* [WPF Disciples] Re: MultiBinding and

>> ------------------------------
>>
>> *From:* Jaime Rodriguez <jai...@microsoft.com>
>> *To:* "wpf-di...@googlegroups.com"
>> <wpf-di...@googlegroups.com>
>> *Sent:* Monday, February 16, 2009 2:41:40 AM
>>
>>
>> *Subject:* [WPF Disciples] Re: MultiBinding and

>> others ?
>>
>>
>>
>> Cheers,
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *From:* wpf-di...@googlegroups.com [mailto:
>> wpf-di...@googlegroups.com] *On Behalf Of *Eric Burke
>> *Sent:* Sunday, February 15, 2009 6:04 AM
>> *To:* wpf-di...@googlegroups.com
>> *Subject:* [WPF Disciples] Re: MultiBinding and

>> IMultiValueConverter in
>> Silverlight?
>>
>>
>>
>> We use this technique heavily in Yahoo! Messenger. Most often
>> it's like
>> this case where you pass in the item you're going to operate on,
>> and then
>> other value(s) that might have to participate in the operation.
>>
>>

>> ------------------------------
>>
>> *From:* "Laurent Bugnion, GalaSoft [MVP, MCP]"
>> <lau...@galasoft.ch>
>> *To:* wpf-di...@googlegroups.com
>> *Sent:* Sunday, February 15, 2009 4:23:33 AM
>> *Subject:* [WPF Disciples] Re: MultiBinding and

>> IMultiValueConverter in
>> Silverlight?
>>
>> Hey Corrado,
>>
>>
>>
>> I rarely use MultiBinding, but there is one case where I find
>> them really
>> useful: When I need to bring an instance of a UI Element in the
>> code. The
>> real life example I have in mind is this (in fact it's funny we
>> talk about
>> it, because I have a blog post half written about this. Credits
>> due to the

>> Doc, who showed me this one day in 2007?)


>>
>>
>>
>> The problem is when you want to apply a resource to an element
>> based on a
>> value. For example, you want to apply a Brush to a Border in a
>> ListBox,
>> depending on the value of a Customer account. If the account is

>> in the 0 ?


>> 1000 range, choose a red-ish brush, 1001-2000 orange-ish brush,
>> 2001-3000

>> Green-ish brush, etc?

>> *From:* wpf-di...@googlegroups.com [mailto:
>> wpf-di...@googlegroups.com] *On Behalf Of *Corrado Cavalli
>> *Sent:* Sunday, February 15, 2009 8:13 AM
>> *To:* wpf-di...@googlegroups.com
>> *Subject:* [WPF Disciples] Re: MultiBinding and
>> IMultiValueConverter in
>> Silverlight?
>>
>>
>>
>> Just wondering?


>>
>> In a M-V-VM world, do we really need MultiBinding?
>>
>>
>>
>> -Corrado
>>
>>
>>

>> *From:* wpf-di...@googlegroups.com [mailto:
>> wpf-di...@googlegroups.com] *On Behalf Of *Laurent Bugnion,
>> GalaSoft
>> [MVP, MCP]
>> *Sent:* sabato 14 febbraio 2009 22:00
>> *To:* wpf-di...@googlegroups.com
>> *Subject:* [WPF Disciples] MultiBinding and IMultiValueConverter

Peter O'Hanlon

unread,
Feb 16, 2009, 11:18:43 AM2/16/09
to wpf-di...@googlegroups.com
That's a nice trick with the namespace. Now I know what I'll be getting my code to do tonight.
--
Peter O'Hanlon
Reply all
Reply to author
Forward
0 new messages