Interesting question. My solution uses ItemTemplateSelector. In its SelectTemplate override, you can access the Panel (VirtualizingStackPanel) under which the ListItems are housed. The trick here is that as each ListItem is added to the ListBox, this override is called and we can use the ChildCount to determinate its index in the ListBox. This helps us in comparison and selecting the right template.
Main.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate
x:Key="DataTemplateWithHighlight">
<StackPanel>
<TextBlock HorizontalAlignment="Stretch"
Text="{Binding Mode=OneWay}"
Background="Gray" />
</StackPanel>
</DataTemplate>
<DataTemplate
x:Key="DataTemplateWithoutHighlight">
<StackPanel>
<TextBlock
HorizontalAlignment="Stretch"
Text="{Binding Mode=OneWay}"
Background="White" />
</StackPanel>
</DataTemplate>
<local:ListBoxItemTemplateSelector
x:Key="listBoxItemTemplateSelector"
HighlightedItemTemplate="{StaticResource DataTemplateWithHighlight}"
NonHighlightedItemTemplate="{StaticResource DataTemplateWithoutHighlight}" />
</Window.Resources>
<StackPanel Orientation="Vertical" d:LayoutOverrides="Height" HorizontalAlignment="Center" VerticalAlignment="Center">
<ListBox
x:Name="listBoxWithMeaninglessNumbers"
Height="100"
Width="100"
ItemsSource="{Binding Data}"
ItemTemplateSelector="{DynamicResource listBoxItemTemplateSelector}">
</ListBox>
</StackPanel>
</Window>
DataItems Class
public class DataItems
{
public List<int> Data { get; set;}
public int HighlightedIndex { get; set; }
}
Some initial data to get you going
public static class DataStub
{
public static DataItems TestDataItems
{
get
{
var dataItems = new DataItems();
dataItems.Data = new List<int>(){1,5,9,6,8};
dataItems.HighlightedIndex = 2;
return dataItems;
}
}
}
MainWindow.Xaml.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
listBoxWithMeaninglessNumbers.DataContext = DataStub.TestDataItems;
}
}
TemplateSelector Class:
public class ListBoxItemTemplateSelector:DataTemplateSelector
{
public DataTemplate NonHighlightedItemTemplate { get; set; }
public DataTemplate HighlightedItemTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var listBoxItem = ((FrameworkElement) container).TemplatedParent as ListBoxItem;
var panel = VisualTreeHelper.GetParent(listBoxItem) as Panel;
var highlightedIndex = (panel.DataContext as DataItems).HighlightedIndex;
var currentChildIndex = panel.Children.Count-1;
return (highlightedIndex == currentChildIndex) ? HighlightedItemTemplate : NonHighlightedItemTemplate;
}
}
}
Hope this helps.