Animate dependency properties

浪尽此生 提交于 2019-12-12 16:08:10

问题


I have a custom user control that registrates a dependency property HoverHeight:

public sealed partial class VirtualPointer : UserControl
{
    public static readonly DependencyProperty HoverHeightProperty =
         DependencyProperty.Register("HoverHeight", typeof(double),
         typeof(VirtualPointer), new PropertyMetadata(1.0,OnHoverHeightChanged));

    public double HoverHeight
    {
        get { return (double)GetValue(HoverHeightProperty); }
        set { SetValue(HoverHeightProperty, value); }
    }
    ...

I use this property to calculate the Margins of some child controls in combination with an according IValueConverter.

In a page that uses this control, I create a Storyboard which should animate the HoverHeight property:

<Page ...>
    <Page.Resources>
        <Storyboard>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="virtualPointer" Storyboard.TargetProperty="HoverHeight">
                <LinearDoubleKeyFrame Value="1.0" KeyTime="0:0:0"/>
                <LinearDoubleKeyFrame Value="0.0" KeyTime="0:0:1"/>
                <LinearDoubleKeyFrame Value="1.0" KeyTime="0:0:2"/>
                <LinearDoubleKeyFrame Value="0.0" KeyTime="0:0:3"/>
                <LinearDoubleKeyFrame Value="1.0" KeyTime="0:0:4"/>
            </DoubleAnimationUsingKeyFrames>
            <!-- other DoubleAnimationUsingKeyFrames -->
        </Storyboard>
    </Page.Resources>
    <!-- ... -->
    <local:VirtualPointer Name="virtualPointer" HoverHeight="0.5"/>    
</Page>

The storyboard contains other animations which work as expected. However, when I start the storyboard the HoverHeight value does not change. Neither the OnHoverHeightChanged handler is called, nor the converter with the new value. I can set a new value with the property setter which in turn calls the OnHoverHeightChanged handler, so there is probably a problem with the animation.

There is no output or exception generated when starting the storyboard.

Am I missing something here? How can I animate the custom dependency property?


回答1:


Set the animation's EnableDependentAnimation property to True.

Dependent animations don't run at all on Windows 8 by default. By default, they are independent animations (such as animations which perform GPU transforms), so you will not be able to receive the change notification on the UI thread. At least that's my understanding.



来源:https://stackoverflow.com/questions/14685379/animate-dependency-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!