【发布时间】:2018-01-09 21:01:35
【问题描述】:
我有一个可选择汽车的主列表,还有一个包含所选汽车 ID 的列表。
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()) ? "√" : "";
}
}
【问题讨论】:
标签: xaml xamarin xamarin.forms