XAML故事板 - 将值绑定到DependencyProperty,它位于同一个控件中

时间:2013-05-23 16:43:06

标签: binding dependency-properties silverlight-5.0

我已经设置了一个带有旋转动画的usercontrol。

在我的故事板中,我有一个值,用于确定控件是向左旋转(-360.0)还是向右旋转(360.0)。 XAML Storyboad中的值如下所示:

<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

    }
}  

1 个答案:

答案 0 :(得分:1)

我很抱歉。正确答案非常简单明了:

这有效:

<EasingDoubleKeyFrame KeyTime="0:0:0.75" Value="{Binding RotationValue}"/>

这不起作用:

<EasingDoubleKeyFrame KeyTime="0:0:0.75" Value="{Binding RelativeSource={RelativeSource Self}, Path=RotationValue}"/>

设置DataContext是正确的:

DataContext="{Binding RelativeSource={RelativeSource Self}}"