views:

77

answers:

1

I am poking around the animation in WPF and I am confused. For RectAnimation, By cannot be negative for height and width.

So, if you use the "By" keyword, rectangles can only grow (instead of "To")?

Here is some example code:

<Page  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
    <Path Stroke="Black" StrokeThickness="1" Fill="LemonChiffon">
      <Path.Data>
        <RectangleGeometry x:Name="myRectangleGeometry" Rect="0,200,100,100" />

      </Path.Data>
      <Path.Triggers>
        <EventTrigger RoutedEvent="Path.Loaded">
          <BeginStoryboard>
            <Storyboard> 
              <!-- Animate the Rect property of the RectangleGeometry
                   which causes the rectangle to animate postion and size. -->
              <RectAnimation
              Storyboard.TargetName="myRectangleGeometry"
              Storyboard.TargetProperty ="Rect"
              Duration="0:0:2" FillBehavior="HoldEnd" 
              From="0,0,100,100"
              By="600,50,200,-50" />
                               ^
            </Storyboard>      |
          </BeginStoryboard>   |
        </EventTrigger>        |
      </Path.Triggers>         |
    </Path>                    |
  </StackPanel>                |
</Page>                        |
                               |
This returns an error, but only if it is negative.
+2  A: 

You're actually defining a rectangle in the By property of the RectAnimation object. A rectangle cannot be created with negative sizes.

By="x,y,Width,Height"

What you're probably wanting to do can be accomplished using the To property instead:

To="600,50,300,50"

md5sum
I knew about the To property, but what if you want to shrink by a relative amount?
Vaccano
I think that's something that Microsoft in their infinite wisdom may have not thought of when they set that up... Either way, even if you're working from code and not from markup, if you need to shrink a rectangle, you'll need to do the appropriate math and use the `To` property instead of the `By` property.
md5sum
I don't like it, but you are right....
Vaccano
Yeah, I don't like it either... I'd like to make shapes in general with negative sizes (aka flipped), but it's just not possible.
md5sum
If you touch your answer I will up vote it (I can't because it says it is too old unless the answer is edited)
Vaccano