Is IMultiValueConverter Available for UWP
Asked Answered
B

3

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?

Breeding answered 23/6, 2016 at 13:29 Comment(0)
P
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}" />
Plagiary answered 14/3, 2020 at 8:15 Comment(0)
M
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

Mcanally answered 23/6, 2016 at 14:27 Comment(2)
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)Appointment
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.Mcanally
A
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

Arras answered 24/6, 2016 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.