WPF ItemsControl ItemsSource 中的当前 ListItem 索引 [英] WPF ItemsControl the current ListItem Index in the ItemsSource

查看:35
本文介绍了WPF ItemsControl ItemsSource 中的当前 ListItem 索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 ItemsControl 中知道当前项目的索引?

编辑这有效!

<x:Array Type="{x:Type sys:String}" x:Key="MyArray"><sys:String>一个</sys:String><sys:String>两个</sys:String><sys:String>三</sys:String></x:数组></Window.Resources><ItemsControl ItemsSource="{StaticResource MyArray}" AlternationCount="100"><ItemsControl.ItemTemplate><数据模板><StackPanel Margin="10"><!-- 一个--><TextBlock Text="{绑定路径=.,StringFormat={}值为 {0}}"/><!-- 两个--><TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex),RelativeSource={RelativeSource TemplatedParent},回退值=失败,StringFormat={}索引为 {0}}"/><!-- 三--><TextBlock Text="{Binding Path=Items.Count,相对来源 ={相对来源 FindAncestor,AncestorType={x:Type ItemsControl}},StringFormat={}Total 是 {0}}"/></StackPanel></数据模板></ItemsControl.ItemTemplate></ItemsControl>

看起来像这样:

解决方案

我不久前问了同样的问题 这里

没有内置的 Index 属性,但您可以将 ItemsControl 的 AlternationCount 设置为高于项目计数的值,并绑定到 AlternationIndex

应该注意的是,如果您的 ListBox 使用虚拟化,则此解决方案可能不起作用,如bradgonesurfing 在此处指出.>

Is it possible to know the current item's Index in a ItemsControl?

EDIT This works!

<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>
            <StackPanel Margin="10">

               <!-- one -->
               <TextBlock Text="{Binding Path=., 
                    StringFormat={}Value is {0}}" />

               <!-- two -->
                <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
                    RelativeSource={RelativeSource TemplatedParent}, 
                    FallbackValue=FAIL, 
                    StringFormat={}Index is {0}}" />

               <!-- three -->
                <TextBlock Text="{Binding Path=Items.Count, 
                    RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type ItemsControl}}, 
                    StringFormat={}Total is {0}}" />

            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

It looks like this:

解决方案

I asked the same thing a while ago here

There isn't a built in Index property, but you can set the AlternationCount of your ItemsControl to something higher than your item count, and bind to the AlternationIndex

<TextBlock Text="{Binding 
    Path=(ItemsControl.AlternationIndex), 
    RelativeSource={RelativeSource Mode=TemplatedParent}, 
    FallbackValue=FAIL, 
    StringFormat={}Index is {0}}" />

It should be noted that this solution may not work if your ListBox uses Virtualization as bradgonesurfing pointed out here.

这篇关于WPF ItemsControl ItemsSource 中的当前 ListItem 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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