Why does ListBox AlternationIndex always return 0

前端 未结 1 831
名媛妹妹
名媛妹妹 2021-01-18 07:36

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 xa

相关标签:
1条回答
  • 2021-01-18 08:25

    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.

    0 讨论(0)
提交回复
热议问题