简体   繁体   中英

Animate selected item of wpf listbox

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> 

为ListBox创建ItemContainerStyle,为ListBoxItem.IsSelected == True添加触发器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM