0

i want to create a custom "To" dependency property of a "DoubleAnimation". It would set the "To" property to the current width of the container Window minus the value. It would be used as follow:

<local:SpecialDoubleAnimation Storyboard.TargetProperty="Width" From="0" SpecialTo="50" Duration="0:0:3"></local:SpecialDoubleAnimation>

And the code of the "SpecialDoubleAnimation":

public class SpecialDoubleAnimation : DoubleAnimation
{

    public static readonly DependencyProperty SpecialToProperty = DependencyProperty.Register("SpecialTo", typeof(double), typeof(SpecialDoubleAnimation),new FrameworkPropertyMetadata(OnChangeCallback));

    public double SpecialTo
    {
        get
        {
            return (double)GetValue(SpecialToProperty);
        }
        set
        {
            SetValue(SpecialToProperty, value);
        }
    }

    private static void OnChangeCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        ((SpecialDoubleAnimation)obj).DoChangeCallback(Window.GetWindow(obj).ActualWidth, (double)args.NewValue);
    }

    private void DoChangeCallback(double windowWidth, double value)
    {
        To = windowWidth - value;
    }
}

As you can see, the "OnChangeCallback" event calls the non-static method "DoChangeCallback", to change the "To" property of the DoubleAnimation, taking the current window width by calling "Window.GetWindow(obj).ActualWidth".

But that code throws an error at the SpecialTo="50" on the XAML code, the error sais

object reference not set to an instance of an object

I think that is because of the call to "Window.GetWindow(obj).ActualWidth", because if i remove that, the error disappears.

How could i implement that? Is there another way to get the actual window size on a "PropertyChangeCallback"?

Thank you.

B.K.
  • 9,982
  • 10
  • 73
  • 105
MorgoZ
  • 2,012
  • 5
  • 27
  • 54
  • 1
    did you try adding something like `if(Window.GetWindow(obj)!=null)...`? – Bizhan Feb 11 '15 at 08:26
  • Thank you @Bizz but inspite the error has disappear, it seems to be always null, do you know why? Any idea to make it work? – MorgoZ Feb 11 '15 at 08:39
  • Alternative solution: use a standard DoubleAnimation and bind its To property to the ActualWidth of the Window. Use a binding converter that subtracts the offset value. The converter may take the offset as parameter. – Clemens Feb 11 '15 at 08:46
  • Please read this post: http://stackoverflow.com/a/14164245/718325 – Bizhan Feb 11 '15 at 09:50
  • Thanks for the answers @Clemens and Bizz i know it could be made using a Converter, but i'd like to know why is "Window.GetWindow(obj)" always returning null. I've also tried to get the SpecialDoubleAnimation's parent with "LogicalTreeHelper.GetParent(obj);" but it also returns null!!!! What is the reason? – MorgoZ Feb 11 '15 at 10:58
  • 1
    I doubt that an animation is added to any logical tree. That's also the reason why GetWindow always returns null. – Clemens Feb 11 '15 at 11:01
  • So, where is an animation added? It is not to in the Visual tree... Then, should i conclude that it is impossible to access the Window or any container from an animation object? – MorgoZ Feb 11 '15 at 11:08
  • I've spent some time looking around, both through visual tree and logical tree and I have to agree with @Clemens on this one. Not sure how you'd get to the Window from an animation. – B.K. Jun 06 '15 at 19:06

0 Answers0