Storyboard TargetProperty binding

本文关键字:binding TargetProperty Storyboard | 更新日期: 2023-09-27 18:19:00

我有一个可以通过拖动移动的控件。当我拖动控件时,我有一个代码,它改变了一个TranslateTransform绑定到的DependencyProperty。现在我需要添加一个按钮,当按下它移动控件,并需要更新DependencyProperty。我可以移动控件,但不知道如何更新DependencyProperty.

背后的代码:

public partial class AirspeedIndicatorView : UserControl
{
    public static readonly DependencyProperty WantedValueProperty =
        DependencyProperty.Register("WantedValue", typeof(double), typeof(AirspeedIndicatorView),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, WantedPropertyChanged));
    public double WantedValue
    {
        get { return (double)GetValue(WantedValueProperty); }
        set { SetValue(WantedValueProperty, value); }
    }
    private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        WantedValue += e.VerticalChange;
    }
}
XAML:

<Thumb Canvas.Top="-6" Height="12" Width="16" DragDelta="Thumb_DragDelta" x:Name="WantedThumb">
    <Thumb.RenderTransform>
        <TranslateTransform Y="{Binding WantedValue,ElementName=View}" />
    </Thumb.RenderTransform>
</Thumb>
<Button Padding="1" Margin="1">
    <Button.Style>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="OverridesDefaultStyle" Value="False" />
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="MoveWanted">
                            <Storyboard Target="{x:Reference WantedThumb}" TargetProperty="RenderTransform.Y" AutoReverse="False">
                                <DoubleAnimation BeginTime="00:00:00" Duration="0:0:0" By="-1" />
                                <DoubleAnimation BeginTime="00:00:00.5" Duration="0:0:1.5" By="-15" />
                                <DoubleAnimation BeginTime="00:00:02" Duration="0:0:1" By="-20" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
                <Trigger Property="IsPressed" Value="False">
                    <Trigger.EnterActions>
                        <StopStoryboard BeginStoryboardName="MoveWanted" />
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Storyboard TargetProperty binding

我现在没有时间检查它,但是你应该能够使用ObjectAnimationUsingKeyFrames类来更新你的DependencyProperty:

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Saved">
    <DiscreteObjectKeyFrame KeyTime="0">
        <DiscreteObjectKeyFrame.Value>
            <System:Double>10.0</System:Double>
        </DiscreteObjectKeyFrame.Value>
    </DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>

实际上,DoubleAnimationUsingKeyFrames类可能对您更好,因为它打算与double s一起工作,但唯一的缺点是我不相信您可以数据绑定数值,因此它必须是硬编码值。从上一个链接页面:

<DoubleAnimationUsingKeyFrames
    Storyboard.TargetName="AnimatedTranslateTransform"
    Storyboard.TargetProperty="X"
    Duration="0:0:6"
    RepeatBehavior="Forever">
    <!-- Using a LinearDoubleKeyFrame, the rectangle moves 
         steadily from its starting position to 500 over 
         the first 3 seconds.  -->
    <LinearDoubleKeyFrame Value="500" KeyTime="0:0:3" />
    <!-- Using a DiscreteDoubleKeyFrame, the rectangle suddenly 
         appears at 400 after the fourth second of the animation. -->
    <DiscreteDoubleKeyFrame Value="400" KeyTime="0:0:4" />
    <!-- Using a SplineDoubleKeyFrame, the rectangle moves 
         back to its starting point. The
         animation starts out slowly at first and then speeds up. 
         This KeyFrame ends after the 6th
         second. -->
    <SplineDoubleKeyFrame KeySpline="0.6,0.0 0.9,0.00" Value="0" KeyTime="0:0:6" />
</DoubleAnimationUsingKeyFrames>