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.