Using Dynamic resource you can achieve this using single listboxitem style
<Window.Resources> <Style x:Key="Lststyle" TargetType="ListBoxItem"> <Setter Property="SnapsToDevicePixels" Value="true"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="Border" Background="Transparent" Padding="7" SnapsToDevicePixels="True"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="ListBox.AlternationIndex" Value="0"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource Color0}"/> </Trigger> <Trigger Property="ListBox.AlternationIndex" Value="1"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource Color1}"/> </Trigger> <Trigger Property="ListBoxItem.IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="Green"/> </Trigger> <Trigger Property="ListBoxItem.IsEnabled" Value="false"> <Setter Property="Foreground" Value="LightGray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <StackPanel > <TextBlock Text="Listbox1"></TextBlock> <ScrollViewer > <ListBox x:Name="lbPersonList" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2"> <ListBox.Resources> <SolidColorBrush x:Key="Color0" Color="#19f39611"></SolidColorBrush> <SolidColorBrush x:Key="Color1" Color="#19000000"></SolidColorBrush> </ListBox.Resources> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> </ListBox> </ScrollViewer> <TextBlock Margin="0,10,0,0" Text="Listbox2"></TextBlock> <ScrollViewer> <ListBox x:Name="lbPersonList1" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2"> <ListBox.Resources> <SolidColorBrush x:Key="Color0" Color="Yellow"></SolidColorBrush> <SolidColorBrush x:Key="Color1" Color="Blue"></SolidColorBrush> </ListBox.Resources> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> </ListBox> </ScrollViewer> </StackPanel>
Simplified xaml
<Window.Resources> <Style x:Key="lst1" TargetType="ListBox" > <Style.Resources> <SolidColorBrush x:Key="Color0" Color="#19f39611"></SolidColorBrush> <SolidColorBrush x:Key="Color1" Color="#19000000"></SolidColorBrush> </Style.Resources> </Style> <Style x:Key="lst2" TargetType="ListBox" > <Style.Resources> <SolidColorBrush x:Key="Color0" Color="Blue"></SolidColorBrush> <SolidColorBrush x:Key="Color1" Color="Yellow"></SolidColorBrush> </Style.Resources> </Style> <Style x:Key="Lststyle" TargetType="ListBoxItem"> <Setter Property="SnapsToDevicePixels" Value="true"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="Border" Background="Transparent" Padding="7" SnapsToDevicePixels="True"> <Border.Style> <Style TargetType="Border"> <Style.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter Property="Background" Value="{DynamicResource Color0}"/> </Trigger> <Trigger Property="ItemsControl.AlternationIndex" Value="1"> <Setter Property="Background" Value="{DynamicResource Color1}"/> </Trigger> </Style.Triggers> </Style> </Border.Style> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="ListBox.AlternationIndex" Value="0"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource Color0}"/> </Trigger> <Trigger Property="ListBox.AlternationIndex" Value="1"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource Color1}"/> </Trigger> <Trigger Property="ListBoxItem.IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="Green"/> </Trigger> <Trigger Property="ListBoxItem.IsEnabled" Value="false"> <Setter Property="Foreground" Value="LightGray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <StackPanel > <TextBlock Text="Listbox1"></TextBlock> <ScrollViewer > <ListBox x:Name="lbPersonList" Style="{StaticResource lst1}" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2">![enter image description here][2] <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> </ListBox> </ScrollViewer> <TextBlock Margin="0,10,0,0" Text="Listbox2"></TextBlock> <ScrollViewer> <ListBox x:Name="lbPersonList1" Style="{StaticResource lst2}" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2"> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> <TextBlock Text="listboxitem1"></TextBlock> </ListBox> </ScrollViewer> </StackPanel>
