Animate selected item of wpf listbox

c#, listview, selecteditem, wpf

Solution

Create ItemContainerStyle for the ListBox and and Add Trigger for ListBoxItem.IsSelected == True

Problem

I'm trying to set a global style for all the listboxes in my application. Below is the xaml code that i've used. Here i've tried to trigger out an animation but it doesn't work. I just want an animation on the selected item. Any help? ``` <Style TargetType="{x:Type ListView}"> <Style.Setters> <Setter Property="BorderThickness" Value="5" /> <Setter Property="FontSize" Value="16" /> <Setter Property="FontFamily" Value="Arial" /> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate x:Name="ListViewItemTemplate"> <TextBlock Text="{Binding}" Padding="0,0,5,5"/> </DataTemplate> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <ControlTemplate.Triggers> <EventTrigger RoutedEvent="ListViewItemBase.Selected"> <BeginStoryboard> <Storyboard TargetProperty="Color"> <ColorAnimation To="#FFFF0000" Duration="0:0:1" AutoReverse="true" /> </Storyboard> </BeginStoryboard> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style.Setters> </Style> ``` Working Version: ``` <Style TargetType="{x:Type ListView}"> <Style.Setters> <Setter Property="BorderThickness" Value="5" /> <Setter Property="FontSize" Value="16" /> <Setter Property="FontFamily" Value="Arial" /> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate x:Name="ListViewItemTemplate"> <TextBlock Text="{Binding}" Padding="0,0,5,5"/> </DataTemplate> </Setter.Value> </Setter> <Setter Property="ItemContainerStyle"> <Setter.Value> <Style> <Style.Triggers> <Trigger Property="ListViewItem.IsSelected" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard Target="ListViewItem" TargetProperty="Background.Color"> <ColorAnimation To="Red" Duration="0:0:0.5" AutoReverse="true" /> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </Style.Triggers> </Style> </Setter.Value> </Setter> </Style.Setters> ```

Original source