Tutorial :Translate XAML EventTrigger to C# Code



Question:

Can anyone tell me how this would look like in C#?

<EventTrigger RoutedEvent="Button.Click" SourceName="btnSplit">                  <EventTrigger.Actions>                      <BeginStoryboard>                          <Storyboard>                              <DoubleAnimation From="0" To="1" Duration="0:0:0.25" Storyboard.TargetName="gridSplit" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>                              <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetName="gridJoin" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>                          </Storyboard>                      </BeginStoryboard>                  </EventTrigger.Actions>              </EventTrigger>  

I don't need the "RoutedEvent"-Part because I want to execute the Code when the User Clicks on a Button programmatically (Click-EventHandler).. The RoutedEvent Handler gets called first which is why I can't do a if-else and need to do the whole Animation in code-behind.

Thanks in advance!

MemphiZ


Solution:1

Solved it!

Just give the Storyboard an x:Name and then call it like this:

<Storyboard x:Name="storyBoardName">      <DoubleAnimation From="0" To="1" Duration="0:0:0.25" Storyboard.TargetName="grid1" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>      <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetName="grid2" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>  </Storyboard>    Storyboard story = (Storyboard)this.FindResource("storyBoardName");    this.BeginStoryboard(story);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »