本文介绍了如何获取当前ItemsControl项目的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 WPF
中获取当前 ItemsControl
项目的索引?
Is there any way to get the index of the current ItemsControl
item in WPF
?
例如,我想做一些例如:
For example, I want to do something like:
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding current_index}">
</TextBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
所以之后,第一个 TextBox
将显示文本0
,第二个1
,第三个2
。
so that after this, the first TextBox
will show text "0"
, second "1"
, third "2" ...
.
推荐答案
我会建议看看:
它解释了如何解决ItemsControl上没有内置的Index属性的事实。
It explains how to work around the fact that there isn't a built in Index property on the ItemsControl.
编辑:
我尝试了以下代码:
<Window.Resources>
<x:Array Type="{x:Type sys:String}" x:Key="MyArray">
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
<sys:String>Three</sys:String>
</x:Array>
</Window.Resources>
<ItemsControl ItemsSource="{StaticResource MyArray}" AlternationCount="100" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex),
RelativeSource={RelativeSource TemplatedParent},
StringFormat={}Index is {0}}">
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl >
TextBlocks如:
And get a window with three TextBlocks like:
[Index is 0]
[Index is 1]
[Index is 2]
这篇关于如何获取当前ItemsControl项目的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!