0

So, what I'm trying to do is access my Form's width and/or height to use in a storyboard. Essentially, I have a Translate Transform animation to slide what are essentially two pages. The animation works fine with hard coded From/To variables, however I need to use soft variables that enable the animation to start from the left/right of my form no matter what size it is.

<Storyboard x:Key="SlideLeftToRight"  
                TargetProperty="RenderTransform.(TranslateTransform.X)"
                AccelerationRatio=".4"
                DecelerationRatio=".4">
     <DoubleAnimation Storyboard.TargetName="PageViewer" Duration="0:0:0.6" From="WindowWidth" To="0"/>
     <DoubleAnimation Storyboard.TargetName="BorderVisual" Duration="0:0:0.6" From="0" To="NegativeWindowWidth"/>
</Storyboard>

However, I have no idea how to do so. Any help is greatly appreciated.

EDIT: I'm guessing it has something to do with:

From="{Binding Width, Source=MainWindow}"

However, when I attempt this, I don't know how to make it negative.

JosephGarrone
  • 4,081
  • 3
  • 38
  • 61

1 Answers1

1

Use ElementName=MainWindow instead, and use ActualWidth instead of Width

From="{Binding ActualWidth, ElementName=MainWindow}"/>

(Make sure you have x:Name=MainWindow in the window, too.

If you want the negative of the current value, you will have to use a Converter.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Side note: don't call a `Window` a 'form', because it is not. – Federico Berasategui Nov 09 '12 at 23:05
  • OK cheers, will try that now. I can't help calling it a Form...Too much time programming in WinForms (Only just started WPF) – JosephGarrone Nov 09 '12 at 23:07
  • Another side note: WPF objects don't have 'variables', they have `Properties`. – Federico Berasategui Nov 09 '12 at 23:07
  • 1
    yet another side note: welcome to the best UI Framework in the History of Mankind =) – Federico Berasategui Nov 09 '12 at 23:09
  • Am I correct in guessing that ActualWidth gives the total width of the whole Window including the border (I have a borderlessw window with a custom border) and that the width gives just the window's width without the border included? From using ActualWidth, my animation now spreads over the custom border. Should I use Width again? – JosephGarrone Nov 09 '12 at 23:11
  • mmm.. nope... I mean if it works ok for you in this case (in the specific case of the Window) just use it, but, whenever you are trying to obtain the "current" width or height of an UIElement you need to use `ActualWidth` or `ActualHeight`. As there are cases where `Width` and `Height` will return 0 or `Double.NaN` – Federico Berasategui Nov 09 '12 at 23:18
  • Take a look at this answer http://stackoverflow.com/questions/607827/what-is-the-difference-between-width-and-actualwidth-in-wpf – Federico Berasategui Nov 09 '12 at 23:19