将边框滑动到屏幕上

时间:2015-11-19 16:35:25

标签: c# wpf

我需要从屏幕底部进行边框滑动到视图中,但是我在获取边框控件ActualHeight时遇到问题。因为这是动画代码我把它放在代码隐藏中,因为它是Views责任。

My Border已将Loaded事件与此相关联:

private void NotifcationWindow_Loaded(object sender, RoutedEventArgs e)
{
    SlideFromBottom(sender);
}

Sender是Border对象,因此SlideFromBottom方法应该能够使用此对象并获得它已经渲染的高度。

public void SlideFromBottom(object sender)
{
    //The Notification container
    Border notification = (sender as Border);

    //The screen size
    var workingArea = SystemParameters.WorkArea;

    Storyboard sb = new Storyboard();
    var animation = new DoubleAnimation()
    {
        BeginTime = TimeSpan.FromSeconds(0),
        Duration = TimeSpan.FromSeconds(5),

        // Get the height and turn it to a negative value, and add screen height. 
        From = (notification.ActualHeight * -1),

        //Slide the border into view
        To = notification.ActualHeight
    };
    Storyboard.SetTarget(animation, notification);
    Storyboard.SetTargetProperty(animation, new PropertyPath("(Margin.Bottom)"));

    sb.Begin();
}

我没有收到任何错误,但动画没有播放,我有什么不对吗?边框垂直对齐到底部,因此负边距应将其从屏幕上取下。

1 个答案:

答案 0 :(得分:1)

您没有看到任何内容的原因是您尚未将动画添加到故事板:

sb.Children.Add(animation);

然后还有一些问题。这样一部分边距不能单独动画。您需要ThicknessAnimation

但是有一个更简单的解决方案。使用RenderTransform。如果为边框指定以下渲染变换:

<Border>
    <Border.RenderTransform>
        <TranslateTransform/>
    </Border.RenderTransform>
</Border>

,然后您可以按如下方式设置动画:

// ...
var animation = new DoubleAnimation()
{
    BeginTime = TimeSpan.FromSeconds(0),
    Duration = TimeSpan.FromSeconds(5),                

    From = notification.ActualHeight,

    To = 0
};
Storyboard.SetTarget(animation, notification);
Storyboard.SetTargetProperty(animation, new PropertyPath("RenderTransform.Y"));

sb.Children.Add(animation);
// ...