我在画布内有一个图像。当加载 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 _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 时,它会覆盖在图像之外,但动画不起作用。