WPF ListView的几个动画(能够优化显示效果)

这个博客内容展示了如何使用XAML为ListView创建透明背景、禁用滚动条并自定义ItemContainerStyle。每个列表项包含一个可缩放的图标和文字,通过MouseEnter和MouseLeave事件实现图标缩放的弹性动画。当选中或取消选中列表项时,颜色会发生变化,提供了一种交互式的用户体验。
摘要由CSDN通过智能技术生成
<ListView Background="Transparent" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectedValue="{Binding SelectedValue}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListViewItem}">
                                    <Grid Height="80" Background="#00000000">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="40*"/>
                                            <RowDefinition Height="20*"/>
                                        </Grid.RowDefinitions>
                                        <Border x:Name="icon" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0 0 0 10" RenderTransformOrigin="0.5,0.5">
                                            <Border.RenderTransform>
                                                <TransformGroup>
                                                    <ScaleTransform x:Name="iconScale"/>
                                                    <SkewTransform/>
                                                    <RotateTransform/>
                                                    <TranslateTransform/>
                                                </TransformGroup>
                                            </Border.RenderTransform>
                                            <Path x:Name="iconPath" Stretch="Fill" Stroke="Transparent" Height="20" Width="20" Data="" Fill="#BBBBBB"/>
                                        </Border>
                                        <Border Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5">
                                            <Border.RenderTransform>
                                                <TransformGroup>
                                                    <ScaleTransform ScaleX="{Binding ElementName=iconScale,Path=ScaleX}" ScaleY="{Binding ElementName=iconScale,Path=ScaleY}"/>
                                                    <SkewTransform/>
                                                    <RotateTransform/>
                                                    <TranslateTransform/>
                                                </TransformGroup>
                                            </Border.RenderTransform>
                                            <TextBlock x:Name="iconText" Text="{Binding}" Foreground="#BBBBBB"/>
                                        </Border>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <EventTrigger RoutedEvent="UIElement.MouseEnter">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="icon">
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="1"/>
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.5">
                                                            <EasingDoubleKeyFrame.EasingFunction>
                                                                <ElasticEase EasingMode="EaseOut" Oscillations="2" Springiness="5"/>
                                                            </EasingDoubleKeyFrame.EasingFunction>
                                                        </EasingDoubleKeyFrame>
                                                    </DoubleAnimationUsingKeyFrames>
                                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="icon">
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="1"/>
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.5">
                                                            <EasingDoubleKeyFrame.EasingFunction>
                                                                <ElasticEase EasingMode="EaseOut" Oscillations="2" Springiness="5"/>
                                                            </EasingDoubleKeyFrame.EasingFunction>
                                                        </EasingDoubleKeyFrame>
                                                    </DoubleAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                        <EventTrigger RoutedEvent="UIElement.MouseLeave">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="icon">
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="1.5"/>
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1">
                                                            <EasingDoubleKeyFrame.EasingFunction>
                                                                <ElasticEase EasingMode="EaseOut" Oscillations="2" Springiness="5"/>
                                                            </EasingDoubleKeyFrame.EasingFunction>
                                                        </EasingDoubleKeyFrame>
                                                    </DoubleAnimationUsingKeyFrames>
                                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="icon">
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="1.5"/>
                                                        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1">
                                                            <EasingDoubleKeyFrame.EasingFunction>
                                                                <ElasticEase EasingMode="EaseOut" Oscillations="2" Springiness="5"/>
                                                            </EasingDoubleKeyFrame.EasingFunction>
                                                        </EasingDoubleKeyFrame>
                                                    </DoubleAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                        <EventTrigger RoutedEvent="ListBoxItem.Selected">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <ColorAnimation To="LightCyan" Duration="0:0:0.2" Storyboard.TargetName="iconPath" Storyboard.TargetProperty="(Path.Stroke).(SolidColorBrush.Color)"/>
                                                    <ColorAnimation To="LightCyan" Duration="0:0:0.2" Storyboard.TargetName="iconText" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"/>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                        <EventTrigger RoutedEvent="ListBoxItem.Unselected">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <ColorAnimation From="Transparent" Duration="0:0:0.2" Storyboard.TargetName="iconPath" Storyboard.TargetProperty="(Path.Stroke).(SolidColorBrush.Color)"/>
                                                    <ColorAnimation To="#BBBBBB" Duration="0:0:0.2" Storyboard.TargetName="iconText" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"/>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="">
                                            <Setter TargetName="iconPath" Property="Data" Value=""/>
                                        </DataTrigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListView.ItemContainerStyle>               
            </ListView>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值