我有一种情况,我想在鼠标左键按下事件上旋转WPF表单上的控件。我试图调整我在其他地方找到的代码,但我还没到达那里。
这就是我所拥有的:
XAML:
<Image
Name="UpArrow"
Height="50"
Width="50"
RenderOptions.BitmapScalingMode="HighQuality"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="10,70,0,0">
<Image.Source>
<TransformedBitmap Source="C:\Some Source File.jpg" >
<TransformedBitmap.Transform>
<RotateTransform Angle="180"/>
</TransformedBitmap.Transform>
</TransformedBitmap>
</Image.Source>
</Image>
在我的代码背后:
Private Sub UpArrow_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UpArrow.MouseLeftButtonDown
Dim TransBitmap As TransformedBitmap = Me.UpArrow.Source
Dim Trans As RotateTransform = TransBitmap.Transform
Dim anim As New Animation.DoubleAnimation(0, New Duration(TimeSpan.FromSeconds(1)))
Trans.BeginAnimation(RotateTransform.AngleProperty, anim)
End Sub
我一直在尝试一些不同的东西,但似乎没有任何效果。我可以很好地阅读C#代码,所以如果你很乐意回答它,那就去吧,我们只是在这里使用VB。我也完全可以在XAML中完成这一切,但我似乎无法让它工作。
谢谢!
答案 0 :(得分:1)
我认为问题只是您尝试动画(transformed bitmap)的对象无法设置动画:
TransformedBitmap实现ISupportInitialize接口以优化多个属性的初始化。属性更改只能在对象初始化期间发生。调用BeginInit表示初始化已经开始,EndInit表示初始化已完成。初始化后,将忽略属性更改。
如果您在图像本身上定位RenderTransform,则相同的代码有效:
<Image
Name="UpArrow"
Height="50"
Width="50"
MouseLeftButtonDown="UpArrow_MouseLeftButtonDown"
RenderOptions.BitmapScalingMode="HighQuality"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="10,70,0,0">
<Image.RenderTransform>
<RotateTransform Angle="180"/>
</Image.RenderTransform>
<Image.Source>
<TransformedBitmap Source="Images/1.png" />
</Image.Source>
</Image>
和
Private Sub UpArrow_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UpArrow.MouseLeftButtonDown
Dim Trans As RotateTransform = Me.UpArrow.RenderTransform
Dim anim As New Animation.DoubleAnimation(0, New Duration(TimeSpan.FromSeconds(1)))
Trans.BeginAnimation(RotateTransform.AngleProperty, anim)
End Sub