Animate Margin / Thickness

帅比萌擦擦* 提交于 2019-11-28 12:12:39

To animate thickness, use a Storyboard like this (from msdn example):

<BeginStoryboard>
    <Storyboard>    
      <!-- BorderThickness animates from left=1, right=1, top=1, and bottom=1 to
      left=28, right=28, top=14, and bottom=14 over one second. -->
      <ThicknessAnimation
        Storyboard.TargetProperty="BorderThickness"
        Duration="0:0:1.5" FillBehavior="HoldEnd" From="1,1,1,1" To="28,14,28,14" />
    </Storyboard>
  </BeginStoryboard>

Actually, to animate any property that takes values as "w,x,y,z" you use a ThicknessAnimation

It seems to me that what you want to do is move the red rectangle to the right.

In that case, put the whole thing in a Canvas and use a DoubleAnimation on the red rectangle's position.

Either way, the error you're getting does not come from the small piece of code you provided, if you want to adress that, please provide use with more code.

Edit: since ThicknessAnimation seems to be not available on WP7, try this instead:

<BeginStoryboard>
    <Storyboard>    
      <DoubleAnimation
        Storyboard.TargetProperty="BorderThickness.Top"
        Duration="0:0:1.5" To="15" />
      <DoubleAnimation
        Storyboard.TargetProperty="BorderThickness.Left"
        Duration="0:0:1.5" To="25" />
    </Storyboard>
  </BeginStoryboard>

I think it's related to what you're doing with the MyRectangleMargin elsewhere in the XAML or code-behind. Remember, Thickness is a struct not a class so you won't be able to use it anywhere that expects a class instance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!