V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
CrystalMoling
V2EX  ›  C#

WPF ListBox 的 Template 绑定 Trigger 动画问题

  •  
  •   CrystalMoling · 2022-08-17 23:12:58 +08:00 · 1063 次点击
    这是一个创建于 589 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有一个用了模板的 ListBox ,现在我需要在选中一个列表项的时候执行一个动画来显示详细信息

    我现在使用的是 SelectionChanged 事件,但是当我更新列表内容的时候也会执行动画

    有没有什么方法能够做到吗

    大致结构:

    <Grid>
        <Grid>
        	<ListBox>
            	<ListBox.Template>
                </ListBox.Template>
            </ListBox>
        </Grid>
        <Grid>
            <!-- 需要执行动画的 Grid -->
        </Grid>
    </Grid>
    
    2 条回复    2022-08-22 11:05:02 +08:00
    yanjinhua
        1
    yanjinhua  
       2022-08-22 11:02:46 +08:00
    方式 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
    yanjinhua
        2
    yanjinhua  
       2022-08-22 11:05:02 +08:00
    方式二:
    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
    源码参考: https://www.v2ex.com/t/871883
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1502 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:20 · PVG 01:20 · LAX 10:20 · JFK 13:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.