Вы можете использовать свойство AlternationIndex
для получения индекса.Просто установите AlternationCount
в ItemsControl
на int.MaxValue
:
<ItemsControl ItemsSource="{Binding yourcollection}" AlternationCount="2147483647">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Если вы передадите элемент в качестве CommandParameter и выполните поиск в своей коллекции, он будет работать, если у вас нет без дубликатов ссылок. в коллекции, в противном случае она не будет выполнена (вы не будете знать, какой экземпляр следует использовать).
Для вложенной коллекции вы можете получить доступ к индексу следующим образом:
<ItemsControl ItemsSource="{Binding ColOfCol}" AlternationCount="2147483647">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding }" AlternationCount="2147483647">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource multivalcnv}">
<Binding Path='(ItemsControl.AlternationIndex)' RelativeSource="{RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}"></Binding>
<Binding Path='(ItemsControl.AlternationIndex)' RelativeSource="{RelativeSource AncestorType=ContentPresenter, AncestorLevel=1}"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public class MultValConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 2)
{
//return (values[0], values[1]); //For the ViewModel
return (values[0], values[1]).ToString(); //For the UI example
}
else
return Binding.DoNothing;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException("It's a one way converter.");
}
}