【问题标题】:Animate width from right to left动画宽度从右到左
【发布时间】:2014-05-02 22:05:35
【问题描述】:

我想为从屏幕右边缘进入的窗口设置动画。

这是我的 xaml

<Window x:Class="SidebarTest.DockWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DockWindow" Width="275"         
    ShowInTaskbar="False" 
    WindowStyle="None" ResizeMode="NoResize"
    AllowsTransparency="True" Background="Transparent" >

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard >
                <DoubleAnimation Duration="0:0:1.5" Storyboard.TargetProperty="Width" From="0" To="275" AccelerationRatio=".1"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

<Grid Background="#2F2F2F">

</Grid>

但它的动画宽度是从左到右而不是从右到左。 像这样:

我怎样才能改变它,让它从边缘进入?

【问题讨论】:

    标签: wpf xaml wpf-animation


    【解决方案1】:

    在您的代码隐藏中,

    public DockWindow()
        {
            InitializeComponent();
            this.Left = this.Width + SystemParameters.FullPrimaryScreenWidth;
        }
    

    将触发器更改为

    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard >
                    <DoubleAnimation Duration="0:0:1.5" Storyboard.TargetProperty="Left"  To="10" AccelerationRatio=".1"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
    

    【讨论】:

      【解决方案2】:

      我建议不要尝试缩放宽度,而是将宽度固定并将TranslateTransform 应用于Window 本身,实际上是从屏幕外将其滑入。下面提供了一个快速而肮脏的示例,您可能希望使用X 坐标和KeyTime 的值来获得恰到好处的效果,但希望这对您有所帮助。干杯

      <Window.RenderTransform>
          <TranslateTransform x:Name="SlideTheThingy" X="1000" />
      </Window.RenderTransform>
      <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
           <BeginStoryboard>
              <Storyboard>
                 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="SlideTheThingy"
                                                Storyboard.TargetProperty="X">
                     <SplineDoubleKeyFrame KeyTime="0:0:1.25" Value="0" />
                 </DoubleAnimationUsingKeyFrames>
              </Storyboard>
           </BeginStoryboard>
        </EventTrigger>
      </Window.Triggers>
      

      【讨论】:

        【解决方案3】:

        窗口按其左上角定位。因此,当您为宽度设置动画并且位置保持不变时,窗口会自然向右增长

        为了获得您想要的效果,您还需要使用.Left 属性为窗口的位置设置动画。如果要将窗口捕捉到屏幕右侧并从那里滑入,则需要使用与屏幕宽度(和零像素宽度)匹配的 Left 值开始动画,然后动画到screenWidth - finalWidthOfWindow.

        【讨论】:

          【解决方案4】:

          我实际上想通了,我需要将水平对齐设置在我想要为其大小设置动画的组件的右侧。它就像一个魅力!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-08-03
            • 2021-11-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-07-09
            • 2019-11-10
            • 2016-09-29
            相关资源
            最近更新 更多