Why does ListBox AlternationIndex always return 0
Asked Answered
B

1

7

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

Bramlett answered 21/10, 2013 at 9:54 Comment(0)
P
24

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, 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.

Polash answered 21/10, 2013 at 10:13 Comment(2)
ok, that makes sense, referencing the ListBoxItem works perfectly, thanks.Bramlett
The counter-intuitive-ness of this framework never seizes to get me. It seems in order to make sense of it you must know it at a level that is practically similar to the one that wrote the library itself. I guess with power comes responsibility.Euclid

© 2022 - 2024 — McMap. All rights reserved.