画布覆盖图像外

时间:2012-08-21 16:53:17

标签: wpf

我在画布中有一个图像。加载UserControl后,图像会向上移动。

<Canvas x:Name="cnvMain"  Width="300" VerticalAlignment="Center" Height="200" SnapsToDevicePixels="True">
    <Image x:Name="Image1"    Width="200"  Stretch="None" Canvas.Bottom="0"  Source="ImageGallery/Desert.jpg" ></Image>
</Canvas>

我使用了DoubleAnimation。

    DoubleAnimation _Animation;
    private Storyboard _StoryBoard;

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {

        _Animation = new DoubleAnimation();
        _Animation.From = -Image1.ActualHeight;
        _Animation.To = cnvMain.ActualHeight;
        _Animation.RepeatBehavior = RepeatBehavior.Forever;
        _Animation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
        _Animation.FillBehavior = FillBehavior.Stop;
        Storyboard.SetTarget(_Animation, Image1);
        Storyboard.SetTargetProperty(_Animation, new PropertyPath(Canvas.BottomProperty));
        _StoryBoard = new Storyboard();
        _StoryBoard.Children.Add(_Animation);
        _StoryBoard.Begin();
    }

此代码运行良好。我的问题是画布没有像图像一样叠加在图像周围(图像尺寸更大的画布,我希望看到画布内的图像区域)。当我将Canvas更改为Grid时,它会叠加在图像外部,但动画不起作用。

1 个答案:

答案 0 :(得分:1)

在您的画布上尝试并使用ClipToBounds ="True"