Это что-то вроде хака. Но вы можете создать ValueConverter, который принимает CollectionView
в качестве параметра и пытается получить индекс из этого:
public class IndexValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is Binding binding &&
value is Scoreboard score &&
binding.Source is CollectionView collectionView &&
collectionView.BindingContext is ScoreboardViewModel viewModel)
{
return viewModel.ScoreboardList.IndexOf(score);
}
return -1;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
Не забудьте зарегистрировать конвертер:
<ContentPage.Resources>
<ResourceDictionary>
<converters:IndexValueConverter x:Key="IndexConverter" />
</ResourceDictionary>
</ContentPage.Resources>
Это как вы можете привязать свой Label
к самому элементу и использовать ссылку в ConverterParameter
для поиска индекса из:
<Label Text="{Binding ., Converter={StaticResource IndexConverter}, ConverterParameter={Binding Source={x:Reference Scores}, Path=BindingContext}}" />
Учитывая, что вы назвали свои CollectionView
"Счеты" с помощью x:Name="Scores"
.
Это дает что-то вроде:
