为什么 ListBox AlternationIndex 总是返回 0 [英] Why does ListBox AlternationIndex always return 0

查看:17
本文介绍了为什么 ListBox AlternationIndex 总是返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我知道还有一些其他类似的问题,但我在让 AlternationIndex 在 ListBox 或 ListView 上工作时遇到了真正的问题.

Ok, I know there are a couple of other similar questions to this but I am having a real issue with getting the AlternationIndex to work on ListBox or ListView.

我的 xaml 是这样的:

my xaml is such:

            <ListBox BorderThickness="0" Name="RecentItemsListBox" HorizontalAlignment="Stretch"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                     ItemsSource="{Binding Path=RecentFilesList}" AlternationCount="100">
                <ListBox.ItemsPanel>

                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
                                    RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource IncCnvrtr}}" 
                                       Foreground="DimGray" FontSize="20" FontWeight="Bold"  
                                       HorizontalAlignment="Left" Margin="5,5,15,5" />
                            <StackPanel VerticalAlignment="Center">
                                <TextBlock Text="{Binding ClassName}" Foreground="Black" />
                                <TextBlock Text="{Binding DisplayName}" Foreground="Black" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

转换器将值增加 1.这工作正常,我已对其进行调试以确认发送到转换器的值始终为 0.

the converter increments the value by 1. This works fine and I have debugged it to confirm the value being sent to the converter is ALWAYS 0.

疯狂的是这仅适用于 ListBox 或 ListView

The crazy thing is this is only for ListBox or ListView

一旦我将它更改为 ItemsControl,索引是正确的,但我不想要项目控件,我想要一个包含所有功能的列表框.

as soon as I change it to an ItemsControl the indexing is correct but I don't want an items control, I want a list box with all the features that come with it.

如果您对为什么会发生这种情况有任何想法,我将非常感谢您的帮助.

If you have any idea as to why this might be happening I'd be grateful for your help.

谢谢

基兰

推荐答案

对于 ListBoxListView 你必须在 ListBoxItem/ListViewItem如下:

For ListBox or ListView you will have to find the property on the ListBoxItem/ListViewItem as below:

     <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
                       RelativeSource={RelativeSource AncestorType=ListBoxItem}, Converter={StaticResource IncCnvrtr}}" 
                       Foreground="DimGray" FontSize="20" FontWeight="Bold"  
                        HorizontalAlignment="Left" Margin="5,5,15,5" />

不同之处在于 ItemsControl 只生成一个 ContentPresenter 成为项目的 Container,并且同一个 ContentPresenter 也在加载 DataTemplate.

The difference is due the fact that ItemsControl only generates a single ContentPresenter which becomes the Container of an item, and the same ContentPresenter is also loading the DataTemplate.

但对于 ListBox,ListBoxItems 是项容器,DataTemplate 将由 ListBoxItem 的 Template 中的 ContentPresenter 加载.所以 ListBoxItemItemsControl.AlternationIndex 属性的值会根据索引而变化,但是 ItemsControl.AlternationIndex 属性的值加载 DataTemplate 的 >ContentPresenter 将始终为 0,这是默认值.

But for ListBox, ListBoxItems are the item containers and DataTemplate will be loaded by the ContentPresenter in Template of ListBoxItem. So value of ListBoxItem's ItemsControl.AlternationIndex property will change according to the index but the value of the ItemsControl.AlternationIndex property of the ContentPresenter that loads the DataTemplate will always be 0, which is the default value.

这篇关于为什么 ListBox AlternationIndex 总是返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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