Animate Margin / Thickness

自古美人都是妖i 提交于 2019-12-17 20:08:05

问题


I just thought I already know how WPF and XAML Syntax works.... wrooong.

I got the message:

'WithEvents' variables can only be typed as classes, interfaces or type parameters with class constraints.

Please, do you know why this syntax is wrong?

I need to use the single value as double. It works later with an storyboard in codebehind.

Regards

I just want to animate the Red rectangle with a storyboard in location and size. Perhaps XAML is the right solution anyway?


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/7292540/animate-margin-thickness

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