I have a master list of selectable cars, and a second list containing the IDs of the selected cars.
public class SelectCarsViewModel : BindableBase
{
public IList<Car> Cars = new List<Car>();
public IList<string> SelectedCars = new List<string>();
}
public class Car
{
public string Id {get; set;}
}
I need to display a check mark next to every selected car. I'm trying to accomplish this by developing a converter which takes the ID of the current car, and the SelectedCars list. I'm having difficulty passing the SelectedCars list from XAML. I'm able to pass SelectCarsPage, but not its BindingContext nor its SelectedCars property.
<ContentPage x:Name="SelectCarsPage">
<ListView ItemsSource=Cars>
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Id, Converter={StaticResource IsCarSelected}, ConverterParameter={Binding Source={x:Reference Name=SelectCarsPage}, Path=SelectedCars}}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
public class IsCarSelected : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//parameter is SelectCarsPage and not the SelectedCars list.
//I'd eventually like to get the following to work
var selectedCars = (List<string>)parameter;
return selectedCars.Contains(value.ToString()) ? "√" : "";
}
}