回答編集履歴

1

見直しキャンペーン中

2023/07/29 12:29

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,253 +1,127 @@
1
1
  先入観で`IsEnabled`や`Visibility`のように状態を切り替えるイメージを持っていたのですが、ボタンで発火するってことは期間があるようなアニメーションなんでしょうか?
2
-
3
-
4
2
 
5
3
  プロパティって言ってしまったのでプロパティでやってみましたが、なんか違う気がします^^;
6
4
 
7
-
8
-
9
- ```xaml
5
+ ```xml
10
-
11
6
  <Window
12
-
13
7
  x:Class="Questions373203.MainWindow"
14
-
15
8
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
16
-
17
9
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
-
19
10
  xmlns:local="clr-namespace:Questions373203"
20
-
21
11
  Width="800"
22
-
23
12
  Height="450"
24
-
25
13
  Button.Click="Window_Button_Click">
26
14
 
27
-
28
-
29
15
  <!-- むりやりEventTrigger使うとこうだがそこまでする?感がw -->
30
-
31
16
  <!--<Window.Triggers>
32
-
33
17
  <EventTrigger RoutedEvent="Button.Click">
34
-
35
18
  <BeginStoryboard>
36
-
37
19
  <Storyboard>
38
-
39
20
  <BooleanAnimationUsingKeyFrames Storyboard.TargetName="userControl1" Storyboard.TargetProperty="IsAnimating">
40
-
41
21
  <DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
42
-
43
22
  </BooleanAnimationUsingKeyFrames>
44
-
45
23
  </Storyboard>
46
-
47
24
  </BeginStoryboard>
48
-
49
25
  </EventTrigger>
50
-
51
26
  </Window.Triggers>-->
52
27
 
28
+ <DockPanel>
29
+ <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
30
+ <CheckBox Content="CheckBox" IsChecked="{Binding IsAnimating, ElementName=userControl1, Mode=TwoWay}" />
31
+ <Button Content="Button" />
32
+ <Button Content="Button" />
33
+ </StackPanel>
34
+ <local:UserControl1 x:Name="userControl1" />
35
+ </DockPanel>
36
+ </Window>
37
+ ```
38
+ ```cs
39
+ using System.Windows;
53
40
 
41
+ namespace Questions373203
42
+ {
43
+ public partial class MainWindow : Window
44
+ {
45
+ public MainWindow() => InitializeComponent();
54
46
 
55
- <DockPanel>
56
-
57
- <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
47
+ private void Window_Button_Click(object sender, RoutedEventArgs e)
58
-
59
- <CheckBox Content="CheckBox" IsChecked="{Binding IsAnimating, ElementName=userControl1, Mode=TwoWay}" />
60
-
61
- <Button Content="Button" />
48
+ => userControl1.IsAnimating = true;
62
-
63
- <Button Content="Button" />
64
-
65
- </StackPanel>
66
-
67
- <local:UserControl1 x:Name="userControl1" />
68
-
69
- </DockPanel>
70
-
71
- </Window>
49
+ }
72
-
50
+ }
73
51
  ```
74
52
 
53
+ ```xml
54
+ <UserControl
55
+ x:Class="Questions373203.UserControl1"
56
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
57
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
58
+ x:Name="root">
59
+ <UserControl.Resources>
60
+ <Storyboard x:Key="MyRoundingAnimation">
61
+ <DoubleAnimationUsingKeyFrames Completed="DoubleAnimationUsingKeyFrames_Completed" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)">
62
+ <LinearDoubleKeyFrame KeyTime="00:00:00.5" Value="360" />
63
+ </DoubleAnimationUsingKeyFrames>
64
+ </Storyboard>
65
+ </UserControl.Resources>
66
+ <Rectangle
67
+ Width="100"
68
+ Height="100"
69
+ RenderTransformOrigin="0.5 0.5"
70
+ Stroke="Red"
71
+ StrokeThickness="5">
72
+ <Rectangle.RenderTransform>
73
+ <TransformGroup>
74
+ <RotateTransform />
75
+ </TransformGroup>
76
+ </Rectangle.RenderTransform>
77
+ <Rectangle.Style>
78
+ <Style>
79
+ <Style.Triggers>
80
+ <DataTrigger Binding="{Binding IsAnimating, ElementName=root}" Value="True">
81
+ <DataTrigger.EnterActions>
82
+ <BeginStoryboard Name="bsb" Storyboard="{StaticResource MyRoundingAnimation}" />
83
+ </DataTrigger.EnterActions>
84
+ <DataTrigger.ExitActions>
85
+ <StopStoryboard BeginStoryboardName="bsb" />
86
+ </DataTrigger.ExitActions>
87
+ </DataTrigger>
88
+ </Style.Triggers>
89
+ </Style>
90
+ </Rectangle.Style>
91
+ </Rectangle>
92
+ </UserControl>
93
+ ```
75
- ```C#
94
+ ```cs
76
-
95
+ using System;
77
96
  using System.Windows;
78
-
97
+ using System.Windows.Controls;
79
-
80
98
 
81
99
  namespace Questions373203
82
-
83
100
  {
84
-
85
- public partial class MainWindow : Window
101
+ public partial class UserControl1 : UserControl
86
-
87
102
  {
88
-
89
- public MainWindow() => InitializeComponent();
90
-
91
-
92
-
93
- private void Window_Button_Click(object sender, RoutedEventArgs e)
94
-
95
- => userControl1.IsAnimating = true;
96
-
97
- }
98
-
99
- }
100
-
101
- ```
102
-
103
-
104
-
105
- ```xaml
106
-
107
- <UserControl
108
-
109
- x:Class="Questions373203.UserControl1"
110
-
111
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
112
-
113
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
114
-
115
- x:Name="root">
116
-
117
- <UserControl.Resources>
118
-
119
- <Storyboard x:Key="MyRoundingAnimation">
120
-
121
- <DoubleAnimationUsingKeyFrames Completed="DoubleAnimationUsingKeyFrames_Completed" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)">
122
-
123
- <LinearDoubleKeyFrame KeyTime="00:00:00.5" Value="360" />
124
-
125
- </DoubleAnimationUsingKeyFrames>
126
-
127
- </Storyboard>
128
-
129
- </UserControl.Resources>
130
-
131
- <Rectangle
132
-
133
- Width="100"
134
-
135
- Height="100"
136
-
137
- RenderTransformOrigin="0.5 0.5"
138
-
139
- Stroke="Red"
140
-
141
- StrokeThickness="5">
142
-
143
- <Rectangle.RenderTransform>
144
-
145
- <TransformGroup>
146
-
147
- <RotateTransform />
148
-
149
- </TransformGroup>
150
-
151
- </Rectangle.RenderTransform>
152
-
153
- <Rectangle.Style>
154
-
155
- <Style>
156
-
157
- <Style.Triggers>
158
-
159
- <DataTrigger Binding="{Binding IsAnimating, ElementName=root}" Value="True">
160
-
161
- <DataTrigger.EnterActions>
162
-
163
- <BeginStoryboard Name="bsb" Storyboard="{StaticResource MyRoundingAnimation}" />
164
-
165
- </DataTrigger.EnterActions>
166
-
167
- <DataTrigger.ExitActions>
168
-
169
- <StopStoryboard BeginStoryboardName="bsb" />
170
-
171
- </DataTrigger.ExitActions>
172
-
173
- </DataTrigger>
174
-
175
- </Style.Triggers>
176
-
177
- </Style>
178
-
179
- </Rectangle.Style>
180
-
181
- </Rectangle>
182
-
183
- </UserControl>
184
-
185
- ```
186
-
187
- ```C#
188
-
189
- using System;
190
-
191
- using System.Windows;
192
-
193
- using System.Windows.Controls;
194
-
195
-
196
-
197
- namespace Questions373203
198
-
199
- {
200
-
201
- public partial class UserControl1 : UserControl
202
-
203
- {
204
-
205
103
  public bool IsAnimating { get => (bool)GetValue(IsAnimatingProperty); set => SetValue(IsAnimatingProperty, value); }
206
-
207
104
  public static readonly DependencyProperty IsAnimatingProperty
208
-
209
105
  = DependencyProperty.Register(nameof(IsAnimating), typeof(bool), typeof(UserControl1),
210
-
211
106
  new FrameworkPropertyMetadata(false));
212
-
213
-
214
107
 
215
108
  public UserControl1() => InitializeComponent();
216
109
 
217
-
218
-
219
110
  private void DoubleAnimationUsingKeyFrames_Completed(object sender, EventArgs e)
220
-
221
111
  {
222
-
223
112
  BeginAnimation(IsAnimatingProperty, null);
224
-
225
113
  IsAnimating = false;
226
-
227
114
  }
228
-
229
115
  }
230
-
231
116
  }
232
-
233
117
  ```
234
-
235
118
  参考
236
-
237
119
  [[C#/WPF] Storyboardの動かし方(Startのさせ方) あれこれ - Qiita](https://qiita.com/tera1707/items/437c3a6747802a196f55)
238
-
239
-
240
120
 
241
121
  [方法: ストーリーボードを使用してアニメーション化した後にプロパティを設定する - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/graphics-multimedia/how-to-set-a-property-after-animating-it-with-a-storyboard?view=netframeworkdesktop-4.8)
242
122
 
243
-
244
-
245
123
  [templates - EventTrigger with Setter in WPF? - Stack Overflow](https://stackoverflow.com/questions/3870214/eventtrigger-with-setter-in-wpf)
246
-
247
-
248
124
 
249
125
  ---
250
126
 
251
-
252
-
253
127
  アニメーションは難しいですね。。。