如何在WPF中向TextBlock的margin属性添加动画 [英] How to add animation to margin property of TextBlock in WPF

查看:163
本文介绍了如何在WPF中向TextBlock的margin属性添加动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样制作TextBlock

I want to make TextBlock like this

但是

aTextBlock.BeginAnimation(Button.MarginProperty, myDoubleAnimation);    

遇到此错误

"System.Windows.Media.Animation.DoubleAnimation"不能用于设置"System.Windows.Thickness"类型的"Margin"属性的动画.

'System.Windows.Media.Animation.DoubleAnimation' cannot be used to animate the 'Margin' property of type 'System.Windows.Thickness'.

我在Xaml中进行了测试,并得到了相同的错误:

I have test in Xaml and get same error:

<Border CornerRadius="8" Background="Red" Margin="352,173,214,368">
            <TextBlock   x:Name="TestB"
                       Text="{Binding ElementName=MTxt,Path=Text,NotifyOnSourceUpdated=True}"
                      Margin="0,0,0,0"
                        HorizontalAlignment="Center"
                       Foreground="White"
                       FontWeight="Bold"
                       FontSize="16">

                <TextBlock.Triggers>
                    <EventTrigger RoutedEvent="TextBlock.Loaded">                       
                            <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation   
                                    Storyboard.TargetName="TestB"
                                    Storyboard.TargetProperty="(TextBlock.Margin)"
                                    To="2" 
                                    Duration="0:0:1" AutoReverse="True"
                                    RepeatBehavior="2" />
                            </Storyboard>
                        </BeginStoryboard>

                    </EventTrigger>
                </TextBlock.Triggers>

            </TextBlock>
        </Border>

你的想法是什么?

推荐答案

您需要使用

You need to use ThicknessAnimation for this. DoubleAnimation is for properties of Double type.

<TextBlock.Triggers>
    <EventTrigger RoutedEvent="TextBlock.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <ThicknessAnimation   
                    Storyboard.TargetName="TestB"
                    Storyboard.TargetProperty="(TextBlock.Margin)"
                    To="2" 
                    Duration="0:0:1" AutoReverse="True"
                    RepeatBehavior="2" />
            </Storyboard>
        </BeginStoryboard>

    </EventTrigger>
</TextBlock.Triggers>

这篇关于如何在WPF中向TextBlock的margin属性添加动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆