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 >
实际运行效果如下图所示:
作者:dongweian
出处:https://www.cnblogs.com/dongweian/p/16390658.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端