我已经设置了一个带有旋转动画的用户控件。
在我的故事板中,我有一个值,它决定了控件是向左(-360.0)还是向右(360.0)旋转。XAML 故事板中的值如下所示:
<EasingDoubleKeyFrame KeyTime="0:0:0.75" Value="-360.0"/>
在后面的代码中,我编写了一个 DependencyProperty,您可以在其中设置一个值(360 或 -360)。看起来像这样:
public static double GetRotationValue(DependencyObject obj)
{
return (double)obj.GetValue(RotationValueProperty);
}
public static void SetRotationValue(DependencyObject obj, double value)
{
obj.SetValue(RotationValueProperty, value);
}
public static readonly DependencyProperty RotationValueProperty= DependencyProperty.RegisterAttached("RotationValue", typeof(double), typeof(RotatingWheel.MainControl), new PropertyMetadata(360.0));
现在,我想使用这个 DependencyProperty 作为值的来源。我尝试了以下方法:
<EasingDoubleKeyFrame KeyTime="0:0:0.75" Value="{Binding RelativeSource={RelativeSource Self}, Path=RotationValue}"/>
那是行不通的。我查看了其他情况并发现,您必须将控件的正确数据上下文设置为“self”。所以我在用户控件的XAML中添加了:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
所以我的问题是:如何将值绑定到位于同一控件内的 DependencyProperty?
-------------------------------------------------- ----
这是完整的 XAML 代码:
<UserControl
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed ="http://schemas.microsoft.com/expression/2010/drawing"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
x:Name="RotatingWheel"
mc:Ignorable="d"
x:Class="RotatingWheel.MainControl"
xmlns:rw="clr-namespace:RotatingWheel"
d:DesignWidth="640" d:DesignHeight="480" Width="100" Height="100">
<UserControl.Resources>
<Storyboard x:Name="Storyboard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="rectangle" RepeatBehavior="Forever">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<!--I want to bind this value to RotationValue DependencyProperty-->
<EasingDoubleKeyFrame KeyTime="0:0:0.75" Value="{Binding RelativeSource={RelativeSource Self}, Path=RotationValue}"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="rectangle1" RepeatBehavior="Forever">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.75" Value="-360.0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ed:Arc ArcThickness="20" ArcThicknessUnit="Pixel" EndAngle="360" Fill="Gray" HorizontalAlignment="Left" Height="100" Margin="0" Stretch="None" Stroke="White" StartAngle="0" UseLayoutRounding="False" VerticalAlignment="Top" Width="100"/>
<Rectangle x:Name="rectangle" Fill="White" HorizontalAlignment="Left" Height="20" Margin="0,40,0,0" Stroke="White" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<CompositeTransform/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="rectangle1" Fill="White" HorizontalAlignment="Left" Height="100" Margin="40,0,0,0" Stroke="White" VerticalAlignment="Top" Width="20" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<CompositeTransform/>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</UserControl>
-------------------------------------------------- ----
这是文件背后的完整代码:
using System.Windows;
using System.Windows.Controls;
namespace RotatingWheel
{
public partial class MainControl : UserControl
{
public MainControl()
{
// Für das Initialisieren der Variablen erforderlich
InitializeComponent();
this.Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);
}
private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
this.Storyboard.Begin();
}
#region DP RotationValue (360 or -360)
public static double GetRotationValue(DependencyObject obj)
{
return (double)obj.GetValue(RotationValueProperty);
}
public static void SetRotationValue(DependencyObject obj, double value)
{
obj.SetValue(RotationValueProperty, value);
}
public static readonly DependencyProperty RotationValueProperty = DependencyProperty.RegisterAttached("RotationValue", typeof(double), typeof(RotatingWheel.MainControl), new PropertyMetadata(360.0));
#endregion
}
}