动画选定的wpf列表框项目

时间:2010-02-13 20:42:17

标签: c# wpf listview selecteditem

我正在尝试为我的应用程序中的所有列表框设置全局样式。下面是我使用过的xaml代码。在这里,我试图触发动画,但它不起作用。我只想在所选项目上制作动画。有什么帮助吗?

<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>

工作版:

<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> 

1 个答案:

答案 0 :(得分:5)

为ListBox创建ItemContainerStyle,为ListBoxItem创建ItemContainerStyle.IsSelected == True