Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this with an EventTrigger.</p> <p>You can define the trigger in a FrameworkElement.Triggers property of any container of both the button and the animation targets.</p> <pre><code> &lt;StackPanel Orientation="Horizontal"&gt; &lt;StackPanel.Triggers&gt; &lt;EventTrigger SourceName="TheButton" RoutedEvent="Button.Click"&gt; &lt;BeginStoryboard&gt; &lt;Storyboard&gt; &lt;ColorAnimation Storyboard.TargetName="LimeRect" Storyboard.TargetProperty="Fill.Color" To="Red" /&gt; &lt;ColorAnimation Storyboard.TargetName="RedRect" Storyboard.TargetProperty="Fill.Color" To="Lime" /&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/EventTrigger&gt; &lt;/StackPanel.Triggers&gt; &lt;Button x:Name="TheButton" Content="Play" /&gt; &lt;Rectangle x:Name="LimeRect" Fill="Lime" Width="50" Height="50" /&gt; &lt;Rectangle x:Name="RedRect" Fill="Red" Width="50" Height="50" /&gt; &lt;/StackPanel&gt; </code></pre> <p>If there is a relative path to your targets, you can use <code>Storyboard.Target="{Binding PathToTarget}"</code> in place of <code>Storyboard.TargetName="TargetName"</code>.</p> <p>EDIT: (see comments)</p> <p>If you are animating the button itself, you can put the triggers right in the button and you don't need any target names.</p> <p>Example - Animating the size of a ToggleButton: </p> <pre><code> &lt;ToggleButton Content="Toggle" Width="50" Height="50"&gt; &lt;ToggleButton.Triggers&gt; &lt;EventTrigger RoutedEvent="ToggleButton.Checked"&gt; &lt;BeginStoryboard&gt; &lt;Storyboard&gt; &lt;DoubleAnimation Duration="00:00:00.2" Storyboard.TargetProperty="Width" To="100" /&gt; &lt;DoubleAnimation Duration="00:00:00.2" Storyboard.TargetProperty="Height" To="100" /&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/EventTrigger&gt; &lt;EventTrigger RoutedEvent="ToggleButton.Unchecked"&gt; &lt;BeginStoryboard&gt; &lt;Storyboard&gt; &lt;DoubleAnimation Duration="00:00:00.2" Storyboard.TargetProperty="Width" To="50" /&gt; &lt;DoubleAnimation Duration="00:00:00.2" Storyboard.TargetProperty="Height" To="50" /&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/EventTrigger&gt; &lt;/ToggleButton.Triggers&gt; &lt;/ToggleButton&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload