首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定到RotateTransform.Angle的WPF不起作用

绑定到RotateTransform.Angle的WPF不起作用
EN

Stack Overflow用户
提问于 2013-09-05 22:39:08
回答 1查看 4.1K关注 0票数 1

我试图制作一个简单的应用程序,在屏幕上弹出一个图像并旋转它。我希望旋转速度是动态的,每当图像击中包含窗口的一侧时,就会发生变化。所以,我试图弄清楚如何动态地改变旋转的速度,甚至是方向。我正在尝试传递一个速度值,并用动画动态地改变旋转。

无论如何,我只是尝试测试通过动画AngleProperty和绑定到XAML中的角度来动态改变旋转的能力。我肯定做错了什么,因为图像不会旋转。

如有任何帮助,我们将不胜感激!

谢谢,柯蒂斯

这是我的XAML:

代码语言:javascript
复制
<UserControl x:Class="Scooter.Bug"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             Loaded="Bug_OnLoaded"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Image x:Name="_image" Source="Images/Author.png" RenderTransformOrigin="0.5, 0.5">
        <Image.RenderTransform>
            <RotateTransform Angle="{Binding Angle}"/>
        </Image.RenderTransform>    
        </Image>
    </Grid>
</UserControl>

这是我的代码隐藏:

代码语言:javascript
复制
using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace Scooter
{
    public partial class Bug
    {
        public static readonly DependencyProperty SpinSpeedProperty = DependencyProperty.Register("SpinSpeed", typeof (TimeSpan), typeof (Bug), new PropertyMetadata(default(TimeSpan)));
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof (double), typeof (Bug), new PropertyMetadata(default(double)));

        public Bug()
        {
            InitializeComponent();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            Angle = Angle >= 360 ? 0 : Angle + 1;
        }

        public TimeSpan SpinSpeed
        {
            get { return (TimeSpan) GetValue(SpinSpeedProperty); }
            set { SetValue(SpinSpeedProperty, value); }
        }

        public double Angle
        {
            get { return (double) GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }
        }

        private void Bug_OnLoaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation animation = new DoubleAnimation
            {
                From = 0,
                To = 360,
                RepeatBehavior = RepeatBehavior.Forever,
                Duration = SpinSpeed
            };

            _image.BeginAnimation(AngleProperty, animation);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-05 23:15:13

您正在调用图像上的BeginAnimation(),但使用的是来自BugAngleProperty

您可以在BeginAnimation()上使用RotateTransform

代码语言:javascript
复制
_image.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, animation);

或者调用控件上的BeginAnimation()

代码语言:javascript
复制
this.BeginAnimation(AngleProperty, animation);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18647065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档