Подтвердить что ты не робот

WPF - привязка к индексу предметов изнутри ItemTemplate of ItemsControl?

Есть ли способ привязки к ItemIndex из ItemTemplate элемента ItemsControl?

Например:

<ItemsControl ItemsSource="{Binding Path=ItemList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=ThisItemsIndex}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
4b9b3361

Ответ 1

Если вы не используете ни одного типа чередующихся стилей строк, вы можете захватить AlternationIndex для этого. Установите AlternationCount на свой ItemsControl на нечто большее, чем максимальное количество ваших предметов, а затем используйте

Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}"

Изменить: поскольку bradgonesurfing указано в комментариях, это не рекомендуется, если вы используете виртуализацию, поскольку она будет индексировать только созданные объекты, а не весь список.

Ответ 2

Вот метод, который я использовал для добавления индекса привязки в элемент коллекции. Я в основном обертываю свой элемент в контейнер, который имеет индекс, и имеет пользовательский ObservableCollection, который принимает оболочку.

Обратите внимание, что MoveItem не переопределяется, но должен быть для полной реализации.

public class IndexedItemContainerCollection<T> : ObservableCollection<IndexedItemContainer<T>>
{
    public IndexedItemContainerCollection()
    {

    }

    public IndexedItemContainerCollection(IEnumerable<IndexedItemContainer<T>> collection)
        : base(collection)
    {
        var index = 0;
        foreach (var item in this)
        {
            item.Index = index;
        }
    }

    protected override void InsertItem(int index, IndexedItemContainer<T> item)
    {
        item.Index = index;
        base.InsertItem(index, item);
        foreach (var indexedItem in this.Where(x=>x.Index > index))
        {
            indexedItem.Index++;
        }
    }

    protected override void RemoveItem(int index)
    {
        base.RemoveItem(index);
        foreach (var indexedItem in this.Where(x => x.Index > index))
        {
            indexedItem.Index--;
        }
    }

}

public class IndexedItemContainer<T>
{
    public int Index { get; set; }
    public T Item { get; set; }
}

Затем я расширяю свой класс-оболочку, чтобы получить свойство bindable, которое я контролирую, как отображается индекс:

public class NamedIndexedItemContainer<T> : IndexedItemContainer<T>
{
    public string Name
    {
        get { return string.Format("Item #{0}", Index + 1); }
    }
}

Пример использования

XAML:

    <ComboBox ItemsSource="{Binding ItemList}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

код:

private IndexedItemContainerCollection<MyItem> _itemList;
public IndexedItemContainerCollection<MyItem> ItemList
{
    get { return _itemList; }
    set { _itemList= value; OnPropertyChanged(); }
}


ItemList = new IndexedItemContainerCollection<MyItem>();
var newItem = new NamedIndexedItemContainer<MyItem>() { Item = new MyItem() { ... } };
ItemList.Add(newItem);

Конечно, любое связывание с фактическим экземпляром MyItem должно пройти через свойство IndexedItemContainer Item.

Ответ 3

Для записи есть еще один способ сделать это: используя собственный Конвертер. Немного сложнее, но вам не нужно беспокоиться о AlternationCount/Index.

public sealed class ArrayWrapperConverter : IValueConverter
{
    private static readonly Type ArrayWrappingHelperType = typeof(ArrayWrappingHelper<>);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return null;
        }

        Type valueType = value.GetType();
        if (!valueType.IsArray)
        {
            return DependencyProperty.UnsetValue;
        }

        Type elementType = valueType.GetElementType();
        Type specificType = ArrayWrappingHelperType.MakeGenericType(elementType);

        IEnumerable wrappingHelper = (IEnumerable) Activator.CreateInstance(specificType, value);
        return wrappingHelper;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

internal class ArrayWrappingHelper<TValue> : IEnumerable
{
    private readonly TValue[] _array;

    public ArrayWrappingHelper(object array)
    {
        _array = (TValue[]) array;
    }

    public IEnumerator GetEnumerator()
    {
        return _array.Select((item, index) => new ArrayItemWrapper<TValue>(_array, index)).GetEnumerator();
    }
}

public class ArrayItemWrapper<TValue>
{
    private readonly TValue[] _array;
    private readonly int _index;

    public int Index 
    {
        get { return _index; }
    }

    public TValue Value
    {
        get { return _array[_index]; }
        set { _array[_index] = value; }
    }

    public ArrayItemWrapper(TValue[] array, int index)
    {
        _array = array;
        _index = index;
    }
}

Использование образца:

<Window x:Class="WpfArrayBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:WpfArrayBinding.Converters"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <c:ArrayWrapperConverter x:Key="ArrayWrapperConverter" />

            <x:Array Type="{x:Type s:String}" x:Key="MyArray">
                <s:String>Foo</s:String>
                <s:String>Bar</s:String>
                <s:String>Baz</s:String>
            </x:Array>
    </ResourceDictionary>
    </Window.Resources>

    <ItemsControl ItemsSource="{Binding Source={StaticResource MyArray}, Converter={StaticResource ArrayWrapperConverter}}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>                
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding Index}" />
                    <TextBox Text="{Binding Value}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>