我有一个可选汽车的主列表,第二个列表包含所选汽车的ID。Xamarin Pass Parent BindingContext转换器的值
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;}
}
我需要在每个选定的汽车旁边显示一个复选标记。我试图通过开发一个转换器来获得当前汽车的ID和SelectedCars
列表。我无法通过XAML的SelectedCars
列表。我能够传递SelectCarsPage,但不能传递它的BindingContext和它的SelectedCars属性。
<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()) ? "√" : "";
}
}