none
WPF前台动画缩放控件 RRS feed

  • 问题

  • 在前台(XAML)对某个控件进行缩放(Width和Height放大一倍),但是执行动画时报错:

    <Border x:Name="checkHandle"
            Width="100"
            Height="40">
        <Border.RenderTransform>
            <TransformGroup>
                <ScaleTransform x:Name="handleScaleX" ScaleX="1"/>
            </TransformGroup>
        </Border.RenderTransform>
    </Border>

    Trigger出发动画:

    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="MyScaleX" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)" To="2"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="MyScaleX" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)" To="1"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.ExitActions>

    正确做法是什么样的?


    • 已编辑 oneonce 2017年8月10日 6:29
    2017年8月10日 6:28

答案

  • Hi,

    >> TransFormGroup里面包含了一个位移动画和缩放动画,就是在一个界面内有个按钮,一边走一边放大,都在XAML中做。

    由于你没有给出你的全部代码,我也不知道你的代码哪里有问题,而且你也没有说动画的触发时机,你可以试试下面的代码。

     <Window.Triggers>
            <EventTrigger RoutedEvent="Window.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation 
                                To="2" 
                                RepeatBehavior="Forever" 
                                Duration="0:0:3"
                                AutoReverse="True"
                                Storyboard.TargetName="myButtonScale"
                                Storyboard.TargetProperty="(ScaleTransform.ScaleX)"/>
                            <DoubleAnimation 
                                Storyboard.TargetName="myButtonTranslate" 
                                Storyboard.TargetProperty="(TranslateTransform.X)" 
                                To="200" 
                                RepeatBehavior="Forever"
                                AutoReverse="True"                         
                                Duration="0:0:3"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Window.Triggers>
        <Canvas>
            <Button x:Name="myButton" Canvas.Top="20" Canvas.Left="20" Height="20" Width="100" Content="Test" >
                <Button.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform x:Name="myButtonScale" CenterX="0" CenterY="0" ScaleX="1" ScaleY="1"/>
                        <TranslateTransform x:Name="myButtonTranslate" X="0" Y="0" />
                    </TransformGroup>
                </Button.RenderTransform>
            </Button>
        </Canvas>

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 oneonce 2017年8月11日 3:27
    2017年8月11日 2:28
    版主

全部回复

  • Hi,

    请提供完整的代码以保证我们可以重现你的问题?以及提供错误信息?以及你想怎样(何时)触发动画?

    注意ExitActions不支持EventTriggers;

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月10日 10:50
    版主
  • Hi,

    请提供完整的代码以保证我们可以重现你的问题?以及提供错误信息?以及你想怎样(何时)触发动画?

    注意ExitActions不支持EventTriggers;

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    TransFormGroup里面包含了一个位移动画和缩放动画,就是在一个界面内有个按钮,一边走一边放大,都在XAML中做。
    2017年8月10日 12:23
  • Hi,

    >> TransFormGroup里面包含了一个位移动画和缩放动画,就是在一个界面内有个按钮,一边走一边放大,都在XAML中做。

    由于你没有给出你的全部代码,我也不知道你的代码哪里有问题,而且你也没有说动画的触发时机,你可以试试下面的代码。

     <Window.Triggers>
            <EventTrigger RoutedEvent="Window.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation 
                                To="2" 
                                RepeatBehavior="Forever" 
                                Duration="0:0:3"
                                AutoReverse="True"
                                Storyboard.TargetName="myButtonScale"
                                Storyboard.TargetProperty="(ScaleTransform.ScaleX)"/>
                            <DoubleAnimation 
                                Storyboard.TargetName="myButtonTranslate" 
                                Storyboard.TargetProperty="(TranslateTransform.X)" 
                                To="200" 
                                RepeatBehavior="Forever"
                                AutoReverse="True"                         
                                Duration="0:0:3"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Window.Triggers>
        <Canvas>
            <Button x:Name="myButton" Canvas.Top="20" Canvas.Left="20" Height="20" Width="100" Content="Test" >
                <Button.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform x:Name="myButtonScale" CenterX="0" CenterY="0" ScaleX="1" ScaleY="1"/>
                        <TranslateTransform x:Name="myButtonTranslate" X="0" Y="0" />
                    </TransformGroup>
                </Button.RenderTransform>
            </Button>
        </Canvas>

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 oneonce 2017年8月11日 3:27
    2017年8月11日 2:28
    版主