ItemControl条目类控件使用Index

在WPF中,为了得到当前ItemsControl的索引,我们可以使用ItemsControl.AlternationIndex附加属性,具体使用为我们先设置所在条目控件的AlternationCount属性为一定数目,然后所在条目控件会自动给每一列按照顺序赋值Index,具体代码如下所示:

 <Window.Resources>
        <x:Array Type="{x:Type system:String}" x:Key="MyArray">
            <system:String>Index-1</system:String>
            <system:String>Index-2</system:String>
            <system:String>Index-3</system:String>
        </x:Array>
    </Window.Resources>
    <ItemsControl ItemsSource="{StaticResource MyArray}" AlternationCount="100" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
               <StackPanel Orientation="Horizontal">
                   <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
                    RelativeSource={RelativeSource TemplatedParent}, 
                    StringFormat={}Index is {0}:}">
                   </TextBlock>
                   <TextBlock Text="{Binding}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl >

实际运行效果如下图所示:

image-20220619160418044

作者:dongweian

出处:https://www.cnblogs.com/dongweian/p/16390658.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   Dwaynerbing  阅读(820)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题