【问题标题】:XAML Storyboard - Binding a value to a DependencyProperty, that lives inside the same controlXAML 故事板 - 将值绑定到位于同一控件中的 DependencyProperty
【发布时间】:2013-05-23 16:43:06
【问题描述】:

我设置了一个带有旋转动画的用户控件。

在我的故事板中,我有一个值,它决定控件是向左 (-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

    }
}  

【问题讨论】:

    标签: binding dependency-properties silverlight-5.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}}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 2017-01-28
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多