模板化动画:我可以在时间轴中引用父属性吗?

时间:2016-01-19 16:39:20

标签: c# wpf xaml animation freezable

给定以下自定义动画类:(从this问题的答案之一):

public class BrushAnimation : AnimationTimeline {
    public Brush From {
        get { return this.GetValue( FromProperty ) as Brush; }
        set { this.SetValue( FromProperty, value ); }
    }
    public Brush To {
        get { return this.GetValue( ToProperty ) as Brush; }
        set { this.SetValue( ToProperty, value ); }
    }

    public static readonly DependencyProperty
        FromProperty = DependencyProperty.Register(
            "From", typeof( Brush ), typeof( BrushAnimation ) ),
        ToProperty = DependencyProperty.Register(
            "To", typeof( Brush ), typeof( BrushAnimation ) );

    public override Type TargetPropertyType {
        get { return typeof( Brush ); }
    }

    public override object GetCurrentValue(
        object defaultOriginValue,
        object defaultDestinationValue,
        AnimationClock animationClock
     ) {
            return this.GCV(
                defaultOriginValue as Brush,
                defaultDestinationValue as Brush,
                animationClock );
    }

    protected override Freezable CreateInstanceCore( ) {
        return new BrushAnimation( );
    }

    public object GCV(
        Brush defaultOriginValue,
        Brush defaultDestinationValue,
        AnimationClock animationClock
     ) {
        if ( !animationClock.CurrentProgress.HasValue )
            return Brushes.Transparent;

        defaultOriginValue = this.From ?? defaultOriginValue;
        defaultDestinationValue = this.To ?? defaultDestinationValue;

        return animationClock.CurrentProgress.Value == 0 ? defaultOriginValue
            : animationClock.CurrentProgress.Value == 1 ? defaultDestinationValue
            : new VisualBrush( new Border( ) {
                Width = 1,
                Height = 1,
                Background = defaultOriginValue,
                Child = new Border( ) {
                    Background = defaultDestinationValue,
                    Opacity = animationClock.CurrentProgress.Value
                }
            } );
    }
}

鉴于以下用例:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:Components="clr-namespace:FooBar.Components"
    xmlns:Animations="clr-namespace:FooBar.Classes">
    <Style TargetType="{x:Type Components:MyButtons}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <!--Stuff-->
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="GotFocus">
                            <BeginStoryboard>
                                <Storyboard>
                                    <Animations:BrushAnimation
                                        Storyboard.TargetName="Foo"
                                        Storyboard.TargetProperty="BarBrush"
                                        Duration="0:0:0.3"
                                        From="<What Do I Put Here?>"
                                        To="<What Do I Put HERE?!>"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

根据我所读到的内容,您无法绑定时间轴From和To值,因为Freezable

我阅读了一个可能的解决方法,但它使用了Tag属性,我认为这个用例可能超出了所提出的解决方案的范围。

有没有办法让时间轴读取我的组件中的值?

0 个答案:

没有答案