5

I am converting a WPF to UWP and it uses a number of IMultiValueConverters. I can't find any reference for IMultiValueConverter.  Is it available in UWP? If not, if there an alternative?

Rich Benner
  • 7,873
  • 9
  • 33
  • 39
IwillMinkin
  • 107
  • 3

3 Answers3

3

A very simple method for binding to multiple values is to use x:Bind with a function. For example,

<TextBlock Text="{x:Bind local:Helper.FormatName(Id, Name)}" />

or

<TextBlock Text="{x:Bind local:Helper.FormatTotal(ViewModel.SelectedWidget.Cost, ViewModel.Quantity, ViewModel.Bundles)}" />

In both of these examples the function is a static function, but the function can also be defined in the code behind or as part of the view model.

<TextBlock Text="{x:Bind CalculateResult(ViewModel.Widget, ViewModel.Foo)}" />
<TextBlock Text="{x:Bind ViewModel.FormatResult(ViewModel.Widget, ViewModel.Foo)}" />

Rather than passing individual values, you can pass a single object that contains multiple properties.

<TextBlock Text="{x:Bind local:Helper.FormatResult(ViewModel.Widget)}" />

Function binding is very flexible and can often be used in place of a converter, creating simpler, more readable code.

<TextBlock Background="{x:Bind local:Helper.GetColor(ViewModel.Widget)}" />

Function binding can also be used with two way bindings by adding a BindBack function.

<TextBox Text="{x:Bind ViewModel.FormatWidget(ViewModel.Widget), BindBack=ViewModel.UpdateWidget}" />
Jeremiah Mercier
  • 451
  • 5
  • 18
2

There is no multiple converter in UWP.

Without Multiple Converter, you can bind to a single property in the ViewModel.

1 That single property should take into account multiple properties from the view model.

Let's call them source properties

2 If any any change is made to a source property, it should raise a PropertyChanged event on the single property.

Regards

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
  • What if you have a `ListView` whose `ItemsSource` is bound to a `Stuff` property on my VM and I want to say change the `Foreground` color based no an item in the collection? I don't want my VM exposing color information (normally we would pass in the color from the XAML side into the converter) –  Dec 29 '18 at 09:31
  • You re right. The VM should not expose Color information. But binding to a VM property and using a Converter that can be parameterized with the ConverterParameter on the binding, or a property of the converter would be decent for me. – Emmanuel DURIN Jan 02 '19 at 08:12
2

There is a very good alternative for this. In the Cimbalino toolkit ( open source on github ) there is a MultiBind behaviour that has been ported to UWP

Details: https://www.pedrolamas.com/2013/05/17/cimbalino-windows-phone-toolkit-multibindingbehavior/ Toolkit code: https://github.com/Cimbalino/Cimbalino-Toolkit

It's also available as nuget, I personally create every UWP app with it, because it has so many great features

Depechie
  • 6,102
  • 24
  • 46