Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate dependency properties

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?

like image 346
Nico Schertler Avatar asked Feb 01 '26 19:02

Nico Schertler


1 Answers

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.

like image 138
Vasiliy Kulakov Avatar answered Feb 03 '26 08:02

Vasiliy Kulakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!