关于父母/容器的动画控制

时间:2013-10-01 13:09:41

标签: wpf animation

我有一个控件,我可以在动画的帮助下从底部移动到最终位置。我现在的问题是我想改变动画的行为,以便它尊重外部容器(DarkGray)。

橙色Ractangle应仅在白色背景上可见,而不是在黑暗的网格上可见!

代码:

MainWindow.xaml:

<Grid Background="DarkGray">
    <Grid Margin="50"
          Background="White">
        <Rectangle x:Name="objectToMove"
                   VerticalAlignment="Bottom"
                   Fill="Orange"
                   Height="50"
                   Width="50"/>
        <Button Height="20"
                Width="40"
                Margin="20"
                Content="Move" 
                Click="Button_Click"
                VerticalAlignment="Top"/>
    </Grid>
</Grid>

MainWindow.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var target = objectToMove;

    var heightOfControl = target.ActualHeight;
    var trans = new TranslateTransform();
    target.RenderTransform = trans;

    var myAnimation = new DoubleAnimation(heightOfControl, 0, TimeSpan.FromMilliseconds(600));

    trans.BeginAnimation(TranslateTransform.YProperty, myAnimation);
}

电流:
Snapshot of the running code snippets

期望的解决方案:
Should be like this

1 个答案:

答案 0 :(得分:1)

请使用ClipToBounds Property

<Grid Margin="50"
      Background="White"
      ClipToBounds="True">