Search code examples
wpfxamlvertical-alignmenteventtrigger

Set VerticalAlignment in an EventTrigger


How would i go about setting a property that doesn't seem to have an animation type associated with it? Specifically, I'd like to change a control's VerticalAlignment whenever an EventTrigger is activated. Here's my current status/failed attempt:

    <EventTrigger RoutedEvent="my:MenuHelper.MenuIsReversed">
          <BeginStoryboard>
              <Storyboard>
                  <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="VerticalAlignment" Storyboard.TargetName="Bouncy_Bar">
                    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Top"/>
                  </ObjectAnimationUsingKeyFrames>
              </Storyboard>
          </BeginStoryboard>
   </EventTrigger>

Which yields this exception:

Cannot animate the 'VerticalAlignment' property on a 'System.Windows.Controls.Border' using a 'System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames'. For details see the inner exception.

Inner exception:

The animation(s) applied to the 'VerticalAlignment' property calculate a current value of 'Top', which is not a valid value for the property.

I'm not sure if i'm improperly qualifying the VerticalAlignment type or if this is simply the wrong way to go about setting an atypical animation property.


Solution

  • Sorry, for the quick answer of my own question. I found that I wasn't laying out the XAML to specify the target type well enough. Here was the working result:

        <EventTrigger RoutedEvent="pill:PillMenuHelper.MenuIsReversed">
              <BeginStoryboard>
                  <Storyboard>
                      <DoubleAnimation Storyboard.TargetName="Bouncy_Bar" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" To="-1" Duration="0:0:.002"/>
                      <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="VerticalAlignment" Storyboard.TargetName="Bouncy_Bar">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0">
                            <DiscreteObjectKeyFrame.Value>
                                <VerticalAlignment>Top</VerticalAlignment>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                      </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
              </BeginStoryboard>
       </EventTrigger>