先入観でIsEnabled
やVisibility
のように状態を切り替えるイメージを持っていたのですが、ボタンで発火するってことは期間があるようなアニメーションなんでしょうか?
プロパティって言ってしまったのでプロパティでやってみましたが、なんか違う気がします^^;
xml
1<Window
2 x:Class="Questions373203.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:local="clr-namespace:Questions373203"
6 Width="800"
7 Height="450"
8 Button.Click="Window_Button_Click">
9
10 <!-- むりやりEventTrigger使うとこうだがそこまでする?感がw -->
11 <!--<Window.Triggers>
12 <EventTrigger RoutedEvent="Button.Click">
13 <BeginStoryboard>
14 <Storyboard>
15 <BooleanAnimationUsingKeyFrames Storyboard.TargetName="userControl1" Storyboard.TargetProperty="IsAnimating">
16 <DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
17 </BooleanAnimationUsingKeyFrames>
18 </Storyboard>
19 </BeginStoryboard>
20 </EventTrigger>
21 </Window.Triggers>-->
22
23 <DockPanel>
24 <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
25 <CheckBox Content="CheckBox" IsChecked="{Binding IsAnimating, ElementName=userControl1, Mode=TwoWay}" />
26 <Button Content="Button" />
27 <Button Content="Button" />
28 </StackPanel>
29 <local:UserControl1 x:Name="userControl1" />
30 </DockPanel>
31</Window>
cs
1using System.Windows;
2
3namespace Questions373203
4{
5 public partial class MainWindow : Window
6 {
7 public MainWindow() => InitializeComponent();
8
9 private void Window_Button_Click(object sender, RoutedEventArgs e)
10 => userControl1.IsAnimating = true;
11 }
12}
xml
1<UserControl
2 x:Class="Questions373203.UserControl1"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 x:Name="root">
6 <UserControl.Resources>
7 <Storyboard x:Key="MyRoundingAnimation">
8 <DoubleAnimationUsingKeyFrames Completed="DoubleAnimationUsingKeyFrames_Completed" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)">
9 <LinearDoubleKeyFrame KeyTime="00:00:00.5" Value="360" />
10 </DoubleAnimationUsingKeyFrames>
11 </Storyboard>
12 </UserControl.Resources>
13 <Rectangle
14 Width="100"
15 Height="100"
16 RenderTransformOrigin="0.5 0.5"
17 Stroke="Red"
18 StrokeThickness="5">
19 <Rectangle.RenderTransform>
20 <TransformGroup>
21 <RotateTransform />
22 </TransformGroup>
23 </Rectangle.RenderTransform>
24 <Rectangle.Style>
25 <Style>
26 <Style.Triggers>
27 <DataTrigger Binding="{Binding IsAnimating, ElementName=root}" Value="True">
28 <DataTrigger.EnterActions>
29 <BeginStoryboard Name="bsb" Storyboard="{StaticResource MyRoundingAnimation}" />
30 </DataTrigger.EnterActions>
31 <DataTrigger.ExitActions>
32 <StopStoryboard BeginStoryboardName="bsb" />
33 </DataTrigger.ExitActions>
34 </DataTrigger>
35 </Style.Triggers>
36 </Style>
37 </Rectangle.Style>
38 </Rectangle>
39</UserControl>
cs
1using System;
2using System.Windows;
3using System.Windows.Controls;
4
5namespace Questions373203
6{
7 public partial class UserControl1 : UserControl
8 {
9 public bool IsAnimating { get => (bool)GetValue(IsAnimatingProperty); set => SetValue(IsAnimatingProperty, value); }
10 public static readonly DependencyProperty IsAnimatingProperty
11 = DependencyProperty.Register(nameof(IsAnimating), typeof(bool), typeof(UserControl1),
12 new FrameworkPropertyMetadata(false));
13
14 public UserControl1() => InitializeComponent();
15
16 private void DoubleAnimationUsingKeyFrames_Completed(object sender, EventArgs e)
17 {
18 BeginAnimation(IsAnimatingProperty, null);
19 IsAnimating = false;
20 }
21 }
22}
参考
[C#/WPF] Storyboardの動かし方(Startのさせ方) あれこれ - Qiita
方法: ストーリーボードを使用してアニメーション化した後にプロパティを設定する - WPF .NET Framework | Microsoft Docs
templates - EventTrigger with Setter in WPF? - Stack Overflow
アニメーションは難しいですね。。。