代码之家  ›  专栏  ›  技术社区  ›  Amsakanna

为wpf列表框的选定项设置动画

  •  3
  • Amsakanna  · 技术社区  · 14 年前

    我正在尝试为应用程序中的所有列表框设置全局样式。下面是我使用的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 回复  |  直到 14 年前
        1
  •  5
  •   Jobi Joy    14 年前

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