回答編集履歴

2

見直しキャンペーン中

2023/08/12 14:58

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,5 +1,5 @@
1
+ `ControlStoryboardAction`を`DataTrigger`でキックすればいいでしょう。
1
2
  [ControlStoryboardAction · microsoft/XamlBehaviorsWpf Wiki](https://github.com/microsoft/XamlBehaviorsWpf/wiki/ControlStoryboardAction)
2
- を`DataTrigger`でキックすればいいでしょう。
3
3
 
4
4
  VMからどう動かすかわからないので、雑にタイマーでランダムに動かしました。
5
5
  データの持ち方(`HorizontalAlignment`等)はご一考ください。

1

見直しキャンペーン中

2023/07/23 08:45

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,315 +1,158 @@
1
1
  [ControlStoryboardAction · microsoft/XamlBehaviorsWpf Wiki](https://github.com/microsoft/XamlBehaviorsWpf/wiki/ControlStoryboardAction)
2
-
3
2
  を`DataTrigger`でキックすればいいでしょう。
4
3
 
4
+ VMからどう動かすかわからないので、雑にタイマーでランダムに動かしました。
5
+ データの持ち方(`HorizontalAlignment`等)はご一考ください。
5
6
 
6
-
7
- VMからどう動かすかわからないので、雑にタイマーでランダムに動かしました。
8
-
9
- データの持ち方(HorizontalAlignment等)はご一考ください。
10
-
11
-
12
-
13
- ```xaml
7
+ ```xml
14
-
15
8
  <Window
16
-
17
9
  x:Class="Questions300617.Views.MainWindow"
18
-
19
10
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
-
21
11
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
-
23
12
  xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
24
-
25
13
  xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
26
-
27
14
  xmlns:vm="clr-namespace:Questions300617.ViewModels"
28
-
29
15
  Width="500"
30
-
31
16
  Height="150">
32
17
 
33
-
34
-
35
18
  <Window.DataContext>
36
-
37
19
  <vm:MainWindowViewModel />
38
-
39
20
  </Window.DataContext>
40
21
 
41
-
42
-
43
22
  <Window.Resources>
44
-
45
23
  <Storyboard x:Key="LR1">
46
-
47
24
  <DoubleAnimation
48
-
49
25
  Storyboard.TargetName="rect"
50
-
51
26
  Storyboard.TargetProperty="(Canvas.Left)"
52
-
53
27
  To="10"
54
-
55
28
  Duration="0:0:0.5" />
56
-
57
29
  </Storyboard>
58
-
59
30
  <Storyboard x:Key="LR2">
60
-
61
31
  <DoubleAnimation
62
-
63
32
  Storyboard.TargetName="rect"
64
-
65
33
  Storyboard.TargetProperty="(Canvas.Left)"
66
-
67
34
  To="70"
68
-
69
35
  Duration="0:0:0.5" />
70
-
71
36
  </Storyboard>
72
-
73
37
  <Storyboard x:Key="LR3">
74
-
75
38
  <DoubleAnimation
76
-
77
39
  Storyboard.TargetName="rect"
78
-
79
40
  Storyboard.TargetProperty="(Canvas.Left)"
80
-
81
41
  To="130"
82
-
83
42
  Duration="0:0:0.5" />
84
-
85
43
  </Storyboard>
86
-
87
44
  <Storyboard x:Key="UD1">
88
-
89
45
  <DoubleAnimation
90
-
91
46
  Storyboard.TargetName="rect"
92
-
93
47
  Storyboard.TargetProperty="(Canvas.Top)"
94
-
95
48
  To="20"
96
-
97
49
  Duration="0:0:0.5" />
98
-
99
50
  </Storyboard>
100
-
101
51
  <Storyboard x:Key="UD2">
102
-
103
52
  <DoubleAnimation
104
-
105
53
  Storyboard.TargetName="rect"
106
-
107
54
  Storyboard.TargetProperty="(Canvas.Top)"
108
-
109
55
  To="40"
110
-
111
56
  Duration="0:0:0.5" />
112
-
113
57
  </Storyboard>
114
-
115
58
  <Storyboard x:Key="UD3">
116
-
117
59
  <DoubleAnimation
118
-
119
60
  Storyboard.TargetName="rect"
120
-
121
61
  Storyboard.TargetProperty="(Canvas.Top)"
122
-
123
62
  To="60"
124
-
125
63
  Duration="0:0:0.5" />
126
-
127
64
  </Storyboard>
128
-
129
65
  </Window.Resources>
130
66
 
131
-
132
-
133
67
  <behaviors:Interaction.Triggers>
134
-
135
68
  <behaviors:EventTrigger EventName="ContentRendered">
136
-
137
69
  <l:LivetCallMethodAction MethodName="Initialize" MethodTarget="{Binding}" />
138
-
70
+ </behaviors:EventTrigger>
71
+ <behaviors:EventTrigger EventName="Closed">
72
+ <l:DataContextDisposeAction />
139
73
  </behaviors:EventTrigger>
140
74
 
141
- <behaviors:EventTrigger EventName="Closed">
142
-
143
- <l:DataContextDisposeAction />
144
-
145
- </behaviors:EventTrigger>
146
-
147
-
148
-
149
75
  <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Left">
150
-
151
76
  <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR1}" />
152
-
77
+ </behaviors:DataTrigger>
78
+ <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Center">
79
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR2}" />
80
+ </behaviors:DataTrigger>
81
+ <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Right">
82
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR3}" />
153
83
  </behaviors:DataTrigger>
154
84
 
155
- <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Center">
85
+ <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Top">
156
-
157
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR2}" />
86
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD1}" />
158
-
159
87
  </behaviors:DataTrigger>
160
-
161
- <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Right">
88
+ <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Center">
162
-
163
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR3}" />
89
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD2}" />
164
-
165
90
  </behaviors:DataTrigger>
166
-
167
-
168
-
169
- <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Top">
91
+ <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Bottom">
170
-
171
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD1}" />
92
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD3}" />
172
-
173
93
  </behaviors:DataTrigger>
174
-
175
- <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Center">
176
-
177
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD2}" />
178
-
179
- </behaviors:DataTrigger>
180
-
181
- <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Bottom">
182
-
183
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD3}" />
184
-
185
- </behaviors:DataTrigger>
186
-
187
94
  </behaviors:Interaction.Triggers>
188
95
 
189
-
190
-
191
96
  <Grid>
192
-
193
97
  <Canvas>
194
-
195
98
  <Rectangle
196
-
197
99
  x:Name="rect"
198
-
199
100
  Canvas.Left="10"
200
-
201
101
  Canvas.Top="20"
202
-
203
102
  Width="40"
204
-
205
103
  Height="20"
206
-
207
104
  Fill="LightBlue"
208
-
209
105
  Stroke="DarkGray"
210
-
211
106
  StrokeThickness="2" />
212
-
213
107
  </Canvas>
214
-
215
108
  </Grid>
216
-
217
109
  </Window>
218
-
219
110
  ```
220
111
 
221
-
222
-
223
- ```C#
112
+ ```cs
224
-
225
113
  using System;
226
-
227
114
  using System.Windows;
228
-
229
115
  using System.Windows.Threading;
230
-
231
116
  using Livet;
232
117
 
233
-
234
-
235
118
  namespace Questions300617.ViewModels
236
-
237
119
  {
238
-
239
120
  public class MainWindowViewModel : ViewModel
240
-
241
121
  {
242
-
243
122
  private readonly Random random = new Random();
244
-
245
123
  private DispatcherTimer timer;
246
124
 
247
-
248
-
249
125
  private HorizontalAlignment _HorizontalAlignment;
250
-
251
126
  public HorizontalAlignment HorizontalAlignment
252
-
253
127
  {
254
-
255
128
  get => _HorizontalAlignment;
256
-
257
129
  set => RaisePropertyChangedIfSet(ref _HorizontalAlignment, value);
258
-
259
130
  }
260
131
 
261
-
262
-
263
132
  private VerticalAlignment _VerticalAlignment;
264
-
265
133
  public VerticalAlignment VerticalAlignment
266
-
267
134
  {
268
-
269
135
  get => _VerticalAlignment;
270
-
271
136
  set => RaisePropertyChangedIfSet(ref _VerticalAlignment, value);
272
-
273
137
  }
274
138
 
275
-
276
-
277
139
  public void Initialize()
278
-
279
140
  {
280
-
281
141
  timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1), };
282
-
283
142
  timer.Tick += (s, e) =>
284
-
285
143
  {
286
-
287
144
  if(random.Next(2) == 0)
288
-
289
145
  {
290
-
291
146
  HorizontalAlignment = (HorizontalAlignment)random.Next(3);
292
-
293
147
  }
294
-
295
148
  else
296
-
297
149
  {
298
-
299
150
  VerticalAlignment = (VerticalAlignment)random.Next(3);
300
-
301
151
  }
302
-
303
152
  };
304
-
305
153
  timer.Start();
306
-
307
154
  }
308
-
309
155
  }
310
-
311
156
  }
312
-
313
157
  ```
314
-
315
158
  長いのでボタンは省略しましたが、あってもOKです。