gpt4 book ai didi

c# - 为什么 AlternationIndex 为 ItemsControl 中的所有项目返回 0?

转载 作者:太空狗 更新时间:2023-10-30 00:20:00 32 4
gpt4 key购买 nike

我正在寻找某种方法来在使用 DataTemplateItemsControl 中显示项目索引。所以我找到了this好问题。我在那个问题中使用了这个想法,但所有的值都是零!我的代码和那个问题中的代码之间的唯一区别是我的控件(将显示索引)不直接在 DataTemplate 中。它在 Grid 中,GridDataTemplate

这是我的代码:

<ItemsControl ItemsSource="{Binding }">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
// column definitions
<Label Content="{Binding RelativeSource={RelativeSource
Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}"/>
// some other controls
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

结果:

0
0
0
// and so on

我希望显示的内容:

0
1
2
// and so on

这段代码有什么问题?

最佳答案

需要设置您的属性的 AlternationCount 属性。

<ItemsControl ItemsSource="{Binding }" AlternationCount={Binding CountOfItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
// column definitions
<Label Content="{Binding RelativeSource={RelativeSource
Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}"
/>
// some other controls
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>

关于c# - 为什么 AlternationIndex 为 ItemsControl 中的所有项目返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15021034/

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