
我有一个用了模板的 ListBox ,现在我需要在选中一个列表项的时候执行一个动画来显示详细信息
我现在使用的是 SelectionChanged 事件,但是当我更新列表内容的时候也会执行动画
有没有什么方法能够做到吗
大致结构:
<Grid> <Grid> <ListBox> <ListBox.Template> </ListBox.Template> </ListBox> </Grid> <Grid> <!-- 需要执行动画的 Grid --> </Grid> </Grid> 1 yanjinhua 2022 年 8 月 22 日 方式 1: XAML: <Grid> <ListBox SelectiOnChanged="ListBox_SelectionChanged"> <ListBoxItem>WPF</ListBoxItem> <ListBoxItem>MAUI</ListBoxItem> </ListBox> <!-- 需要执行动画的 Grid --> <Image x:Name="myImage" Source="fys.png" Stretch="Uniform" Width="40" Height="40"> <Image.RenderTransform> <ScaleTransform x:Name="myScaleTransform" ScaleX="0" ScaleY="0"/> </Image.RenderTransform> </Image> </Grid> cs: SelectionChanged 事件: private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var animation = new DoubleAnimation { From = 0, To = 1.2, Duration = TimeSpan.FromSeconds(1),//比如三秒 }; //也可将动画执行 RepeatBehavior="Forever",当耗时任务完成去做关闭; myScaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, animation); myScaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, animation); } 也可以把图片做 Angle 旋转 loading |
2 yanjinhua 2022 年 8 月 22 日 方式二: XAML 使用现成的 ~~~xml <Grid> <ListBox SelectiOnChanged="ListBox_SelectionChanged"> <ListBoxItem>WPF</ListBoxItem> <ListBoxItem>MAUI</ListBoxItem> </ListBox> <!-- 需要执行动画的 Grid --> <wpfdev:RingLoading x:Name="myRingLoading" Width="160" Height="160" Visibility="Collapsed" VerticalAlignment="Center" HorizOntalAlignment="Center"> </wpfdev:RingLoading> </Grid> ~~~ ~~~C# private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { myRingLoading.Visibility = Visibility.Visible; myRingLoading.IsStart = true; } ~~~ 源码参考: https://www.cnblogs.com/yanjinhua/p/16571359.html 源码参考: t/871883 |