WPF - 从 ItemsControl 的 ItemTemplate 绑定到项目索引? [英] WPF - Bind to Item Index from within ItemTemplate of ItemsControl?

查看:57
本文介绍了WPF - 从 ItemsControl 的 ItemTemplate 绑定到项目索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 ItemsControl 的 ItemTemplate 中绑定到 ItemIndex?

Is there a way to bind to the ItemIndex from within the ItemTemplate of an ItemsControl?

例如:

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

推荐答案

如果您不使用任何类型的交替行样式,您可能能够为此劫持 AlternationIndex.将 ItemsControl 上的 AlternationCount 设置为大于项目的最大可能计数,然后使用

If you're not using any type of alternating row styles you might be able to hijack the AlternationIndex for this. Set AlternationCount on your ItemsControl to something greater than the max possible count of your items and then use

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

正如 bradgonesurfing 在评论,如果您使用虚拟化,则不建议这样做,因为它只会索引生成的项目,而不是整个列表.

As bradgonesurfing pointed out in comments, this is not recommended if you're using virtualization, as it will only index the items that are generated and not the entire list.

这篇关于WPF - 从 ItemsControl 的 ItemTemplate 绑定到项目索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆