Nejc Galof
Nejc Galof

Reputation: 2606

PointAnimation without used triggers

I want to use pointanimation on rectangle. I work in WPF. If i create storyboard and write only DoubleAnimation works. If I add PointAnimation system crash and write to me.

The invocation of the constructor on type 'example.MainWindow' that matches the specified binding constraints threw an exception.'

Is here option if this works without triggers and why this not work?

<Window.Resources>
  <Storyboard x:Key="move">
    <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Width" To="100" Duration="0:0:2"></DoubleAnimation>
    <PointAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Center" To="100,100" Duration="0:0:2"></PointAnimation>
  </Storyboard>    
</Window.Resources>

Upvotes: 0

Views: 315

Answers (1)

pushpraj
pushpraj

Reputation: 13669

the issue with your storyboard is there is no Center property of type point which you can animate.

the actual error behind

The invocation of the constructor on type 'example.MainWindow' that matches the specified binding constraints threw an exception.'

is

Cannot resolve all property references in the property path 'Center'. Verify that applicable objects support the properties.

below is an example using Canvas.Left and Canvas.Top to achieve similar behavior

    <Storyboard x:Key="move">
        <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Width" To="100" Duration="0:0:2"></DoubleAnimation>
        <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="(Canvas.Left)" To="100" Duration="0:0:2"></DoubleAnimation>
        <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="(Canvas.Top)" To="100" Duration="0:0:2"></DoubleAnimation>
    </Storyboard>

Setting location using PointAnimation example

define a class with attached property to hold the value

class RectangleHelper : DependencyObject
{
    public static Point GetLocation(DependencyObject obj)
    {
        return (Point)obj.GetValue(LocationProperty);
    }

    public static void SetLocation(DependencyObject obj, Point value)
    {
        obj.SetValue(LocationProperty, value);
    }

    // Using a DependencyProperty as the backing store for Location.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LocationProperty =
        DependencyProperty.RegisterAttached("Location", typeof(Point), typeof(RectangleHelper), new PropertyMetadata(new Point(), OnLocationChanged));


    private static void OnLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Point newValue = (Point)e.NewValue;

        d.SetValue(Canvas.LeftProperty, newValue.X);
        d.SetValue(Canvas.TopProperty, newValue.Y);
    }
}

in the above code I am manipulating the Canvas.Left and Canvas.Top on rectangle in the property change handler

then xaml as

<Storyboard x:Key="move">
    <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Width" From="1" To="100" Duration="0:0:2"></DoubleAnimation>
    <PointAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="(l:RectangleHelper.Location)" To="100,100" Duration="0:0:2"></PointAnimation>
</Storyboard>

I made example for Location, you may achieve Center with some calculations too.

Upvotes: 1

Related Questions