Multi selection list view using MvxListView with MvvmCross

787 Views Asked by At

I have a MvxListView with multiselection mode. I want to toggle its clicked item color. If it is a listview then I can do it by code on view. But how can I do same task in MVVMCross ? Because MvxListView Itemclick is bind on View Model and here we not get clicked item object not get. Please help

enter image description here

1

There are 1 best solutions below

5
Mark Verkiel On

You can use a ValueConverter and the plugin MvvmCross.Plugin.Color ( can be found in NuGet ).

The ValueConverter can translate a Boolean to a Color for example:

public class BlackOrBlueColorValueConverter : MvxColorValueConverter<bool>
{
    protected override MvxColor Convert(bool value, object parameter, CultureInfo culture)
    {
        if (value)
            return MvxColors.Black;
        else
            return MvxColors.Blue;
    }
}

Create a property Selected within the class that is bound to the MvxListView

private bool _selected
public bool Selected
{
    get
    {
        return _selected;
    }
    set { _selected = value; RaisePropertyChanged(() => Selected); }
}

Bind the property in your Axml

local:MvxBind="BackgroundColor BlackOrBlueColor(Selected);"

On the ItemClickCommand set the property Selected to false or true and the color changes.

For more information about value converters have a look at MvvmCross ValueConverters