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

查看:216
本文介绍了WPF - 从ItemControl的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>


推荐答案

如果您不使用任何类型的交替行您可能会为此而劫持AlternateIndex的风格。将ItemsControl上的AlternateCount设置为大于项目最大可能数量的值,然后使用

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 - 从ItemControl的ItemTemplate内绑定到项目索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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