Question

Hello MVVMCross community,

My question is about ValueConverters parameters:

Is there any way to pass a variable to a Value Converter rather than a constant value as a ConverterParameter?

Something like:

 <EditText
    android:id="@+id/editPrice"
    ...
    local:MvxBind="Text Price; Enabled IsPriceEnabled; BackgroundColor IsPriceEnabled, Converter=Enabled2MvxColor, ConverterParameter=Price"/>

or even pass the whole object for example:

<EditText
    android:id="@+id/editPrice"
    ...
    local:MvxBind="Text Price; Enabled IsPriceEnabled; BackgroundColor IsPriceEnabled, Converter=Enabled2MvxColor, ConverterParameter=editPrice"/>

TIA,

Was it helpful?

Solution

When using Windows/XAML IValueConverter you can't really pass data-bound entries into the value converter parameter.

However, using the Tibet binding extensions within MvvmCross, you should be able to do this if you use the form:

local:MvxBind="BackgroundColor Enabled2MvxColor(IsPriceEnabled, Price)"

For more on this - and on whole object binding, see https://github.com/MvvmCross/MvvmCross/wiki/Databinding

OTHER TIPS

I had to do something similar couple of days ago: it's rather convoluted:

<controls:OptionItemControl 
ItemTitle="{Binding Path=AccountSettings, Converter={StaticResource ContentItemConverter}, Mode=OneTime, 
ConverterParameter=DataRoaming.Text}"
ItemInfo="{Binding Path=AccountSettings, Converter={StaticResource ContentItemConverter}, Mode=OneTime, 
ConverterParameter=DataRoaming.Info}"
ItemValue="{Binding AccountSettings.DataRoaming, Mode=TwoWay}"
/>

So my ViewModel has a property AccountSettings, which is a class that has another property DataRoaming, which has several properties like .Text, .Info

I am sure there is much easier way to do what I needed it to do, but I wanted to get from using magic string; I didn't get it at the end, but at-least this makes it easier for me to read and figure out.

so in converter I get the parameter, split it; then from value type and I can navigate through the properties and get the actual value from the class. Of course I could have as well called a method.

Hope this might give you some ideas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top