gpt4 book ai didi

c# - 如何获取当前 ItemsControl 项的索引?

转载 作者:IT王子 更新时间:2023-10-29 04:52:44 27 4
gpt4 key购买 nike

有什么方法可以获取WPF中当前ItemsControl项的索引?

例如,我想做这样的事情:

<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding current_index}">
</TextBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

这样之后,第一个 TextBox 将显示文本 "0",第二个 "1",第三个 "2 “...

最佳答案

我建议看看:

WPF ItemsControl the current ListItem Index in the ItemsSource

它解释了如何解决 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 >

并获得一个包含三个 TextBlock 的窗口,例如:

[Index is 0]
[Index is 1]
[Index is 2]

关于c# - 如何获取当前 ItemsControl 项的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com