为什么ListBox AlternationIndex总是返回0 [英] Why does ListBox AlternationIndex always return 0
问题描述
好吧,我知道还有其他一些类似的问题,但是让AlternateIndex在ListBox或ListView上工作确实是一个真正的问题.
我的xaml是这样的:
<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.
疯狂的是,这仅适用于ListBox或ListView
一旦我将其更改为ItemsControl,索引编制就正确了,但是我不希望有项目控件,而是想要一个带有所有功能的列表框.
如果您对为什么会发生这种情况有任何想法,我将非常感谢您的帮助.
谢谢
基兰
对于ListBox
或ListView
,您将必须在ListBoxItem
/ListViewItem
上找到属性,如下所示:
<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而成为一个项目的容器,而相同的ContentPresenter
也正在加载DataTemplate.
但是对于ListBox
,ListBoxItem
是项目容器,并且DataTemplate
将由ListBoxItem
的Template
中的ContentPresenter
加载.因此,ListBoxItem
的ItemsControl.AlternationIndex
属性的值将根据索引而变化,但是加载DataTemplate
的ContentPresenter
的ItemsControl.AlternationIndex
属性的值将始终为0,这是默认值. /p>
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.
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>
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.
The crazy thing is this is only for ListBox or ListView
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.
Thanks
Kieran
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" />
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.
But for ListBox
, ListBoxItem
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屋!