常见的方法是使用 ItemToIndexConverter,但是,这有以下缺陷:
无论如何,为了让您开始,您可以在 XAML 中编写以下内容:
<Label
FontAttributes="Bold"
Text="{MultiBinding {Binding Ingredients, Source={Reference mainPage}},
{Binding .},
Converter={local:IndexToItemConverter}}"
VerticalOptions="Center" />
然后,您可以按如下方式实现 IndexToItemConverter:
public class IndexToItemConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
=> values[0] is ObservableCollection<string> c && values[1] is string s ? c.IndexOf(s) : -1;
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}