teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

見直しキャンペーン中

2023/08/12 14:58

投稿

TN8001
TN8001

スコア10104

answer 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

スコア10104

answer CHANGED
@@ -1,158 +1,158 @@
1
- [ControlStoryboardAction · microsoft/XamlBehaviorsWpf Wiki](https://github.com/microsoft/XamlBehaviorsWpf/wiki/ControlStoryboardAction)
2
- を`DataTrigger`でキックすればいいでしょう。
3
-
4
- VMからどう動かすかわからないので、雑にタイマーでランダムに動かしました。
5
- データの持ち方(HorizontalAlignment等)はご一考ください。
6
-
7
- ```xaml
8
- <Window
9
- x:Class="Questions300617.Views.MainWindow"
10
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
11
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
12
- xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
13
- xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
14
- xmlns:vm="clr-namespace:Questions300617.ViewModels"
15
- Width="500"
16
- Height="150">
17
-
18
- <Window.DataContext>
19
- <vm:MainWindowViewModel />
20
- </Window.DataContext>
21
-
22
- <Window.Resources>
23
- <Storyboard x:Key="LR1">
24
- <DoubleAnimation
25
- Storyboard.TargetName="rect"
26
- Storyboard.TargetProperty="(Canvas.Left)"
27
- To="10"
28
- Duration="0:0:0.5" />
29
- </Storyboard>
30
- <Storyboard x:Key="LR2">
31
- <DoubleAnimation
32
- Storyboard.TargetName="rect"
33
- Storyboard.TargetProperty="(Canvas.Left)"
34
- To="70"
35
- Duration="0:0:0.5" />
36
- </Storyboard>
37
- <Storyboard x:Key="LR3">
38
- <DoubleAnimation
39
- Storyboard.TargetName="rect"
40
- Storyboard.TargetProperty="(Canvas.Left)"
41
- To="130"
42
- Duration="0:0:0.5" />
43
- </Storyboard>
44
- <Storyboard x:Key="UD1">
45
- <DoubleAnimation
46
- Storyboard.TargetName="rect"
47
- Storyboard.TargetProperty="(Canvas.Top)"
48
- To="20"
49
- Duration="0:0:0.5" />
50
- </Storyboard>
51
- <Storyboard x:Key="UD2">
52
- <DoubleAnimation
53
- Storyboard.TargetName="rect"
54
- Storyboard.TargetProperty="(Canvas.Top)"
55
- To="40"
56
- Duration="0:0:0.5" />
57
- </Storyboard>
58
- <Storyboard x:Key="UD3">
59
- <DoubleAnimation
60
- Storyboard.TargetName="rect"
61
- Storyboard.TargetProperty="(Canvas.Top)"
62
- To="60"
63
- Duration="0:0:0.5" />
64
- </Storyboard>
65
- </Window.Resources>
66
-
67
- <behaviors:Interaction.Triggers>
68
- <behaviors:EventTrigger EventName="ContentRendered">
69
- <l:LivetCallMethodAction MethodName="Initialize" MethodTarget="{Binding}" />
70
- </behaviors:EventTrigger>
71
- <behaviors:EventTrigger EventName="Closed">
72
- <l:DataContextDisposeAction />
73
- </behaviors:EventTrigger>
74
-
75
- <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Left">
76
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR1}" />
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}" />
83
- </behaviors:DataTrigger>
84
-
85
- <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Top">
86
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD1}" />
87
- </behaviors:DataTrigger>
88
- <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Center">
89
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD2}" />
90
- </behaviors:DataTrigger>
91
- <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Bottom">
92
- <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD3}" />
93
- </behaviors:DataTrigger>
94
- </behaviors:Interaction.Triggers>
95
-
96
- <Grid>
97
- <Canvas>
98
- <Rectangle
99
- x:Name="rect"
100
- Canvas.Left="10"
101
- Canvas.Top="20"
102
- Width="40"
103
- Height="20"
104
- Fill="LightBlue"
105
- Stroke="DarkGray"
106
- StrokeThickness="2" />
107
- </Canvas>
108
- </Grid>
109
- </Window>
110
- ```
111
-
112
- ```C#
113
- using System;
114
- using System.Windows;
115
- using System.Windows.Threading;
116
- using Livet;
117
-
118
- namespace Questions300617.ViewModels
119
- {
120
- public class MainWindowViewModel : ViewModel
121
- {
122
- private readonly Random random = new Random();
123
- private DispatcherTimer timer;
124
-
125
- private HorizontalAlignment _HorizontalAlignment;
126
- public HorizontalAlignment HorizontalAlignment
127
- {
128
- get => _HorizontalAlignment;
129
- set => RaisePropertyChangedIfSet(ref _HorizontalAlignment, value);
130
- }
131
-
132
- private VerticalAlignment _VerticalAlignment;
133
- public VerticalAlignment VerticalAlignment
134
- {
135
- get => _VerticalAlignment;
136
- set => RaisePropertyChangedIfSet(ref _VerticalAlignment, value);
137
- }
138
-
139
- public void Initialize()
140
- {
141
- timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1), };
142
- timer.Tick += (s, e) =>
143
- {
144
- if(random.Next(2) == 0)
145
- {
146
- HorizontalAlignment = (HorizontalAlignment)random.Next(3);
147
- }
148
- else
149
- {
150
- VerticalAlignment = (VerticalAlignment)random.Next(3);
151
- }
152
- };
153
- timer.Start();
154
- }
155
- }
156
- }
157
- ```
1
+ [ControlStoryboardAction · microsoft/XamlBehaviorsWpf Wiki](https://github.com/microsoft/XamlBehaviorsWpf/wiki/ControlStoryboardAction)
2
+ を`DataTrigger`でキックすればいいでしょう。
3
+
4
+ VMからどう動かすかわからないので、雑にタイマーでランダムに動かしました。
5
+ データの持ち方(`HorizontalAlignment`等)はご一考ください。
6
+
7
+ ```xml
8
+ <Window
9
+ x:Class="Questions300617.Views.MainWindow"
10
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
11
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
12
+ xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
13
+ xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
14
+ xmlns:vm="clr-namespace:Questions300617.ViewModels"
15
+ Width="500"
16
+ Height="150">
17
+
18
+ <Window.DataContext>
19
+ <vm:MainWindowViewModel />
20
+ </Window.DataContext>
21
+
22
+ <Window.Resources>
23
+ <Storyboard x:Key="LR1">
24
+ <DoubleAnimation
25
+ Storyboard.TargetName="rect"
26
+ Storyboard.TargetProperty="(Canvas.Left)"
27
+ To="10"
28
+ Duration="0:0:0.5" />
29
+ </Storyboard>
30
+ <Storyboard x:Key="LR2">
31
+ <DoubleAnimation
32
+ Storyboard.TargetName="rect"
33
+ Storyboard.TargetProperty="(Canvas.Left)"
34
+ To="70"
35
+ Duration="0:0:0.5" />
36
+ </Storyboard>
37
+ <Storyboard x:Key="LR3">
38
+ <DoubleAnimation
39
+ Storyboard.TargetName="rect"
40
+ Storyboard.TargetProperty="(Canvas.Left)"
41
+ To="130"
42
+ Duration="0:0:0.5" />
43
+ </Storyboard>
44
+ <Storyboard x:Key="UD1">
45
+ <DoubleAnimation
46
+ Storyboard.TargetName="rect"
47
+ Storyboard.TargetProperty="(Canvas.Top)"
48
+ To="20"
49
+ Duration="0:0:0.5" />
50
+ </Storyboard>
51
+ <Storyboard x:Key="UD2">
52
+ <DoubleAnimation
53
+ Storyboard.TargetName="rect"
54
+ Storyboard.TargetProperty="(Canvas.Top)"
55
+ To="40"
56
+ Duration="0:0:0.5" />
57
+ </Storyboard>
58
+ <Storyboard x:Key="UD3">
59
+ <DoubleAnimation
60
+ Storyboard.TargetName="rect"
61
+ Storyboard.TargetProperty="(Canvas.Top)"
62
+ To="60"
63
+ Duration="0:0:0.5" />
64
+ </Storyboard>
65
+ </Window.Resources>
66
+
67
+ <behaviors:Interaction.Triggers>
68
+ <behaviors:EventTrigger EventName="ContentRendered">
69
+ <l:LivetCallMethodAction MethodName="Initialize" MethodTarget="{Binding}" />
70
+ </behaviors:EventTrigger>
71
+ <behaviors:EventTrigger EventName="Closed">
72
+ <l:DataContextDisposeAction />
73
+ </behaviors:EventTrigger>
74
+
75
+ <behaviors:DataTrigger Binding="{Binding HorizontalAlignment}" Value="Left">
76
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource LR1}" />
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}" />
83
+ </behaviors:DataTrigger>
84
+
85
+ <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Top">
86
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD1}" />
87
+ </behaviors:DataTrigger>
88
+ <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Center">
89
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD2}" />
90
+ </behaviors:DataTrigger>
91
+ <behaviors:DataTrigger Binding="{Binding VerticalAlignment}" Value="Bottom">
92
+ <behaviors:ControlStoryboardAction Storyboard="{StaticResource UD3}" />
93
+ </behaviors:DataTrigger>
94
+ </behaviors:Interaction.Triggers>
95
+
96
+ <Grid>
97
+ <Canvas>
98
+ <Rectangle
99
+ x:Name="rect"
100
+ Canvas.Left="10"
101
+ Canvas.Top="20"
102
+ Width="40"
103
+ Height="20"
104
+ Fill="LightBlue"
105
+ Stroke="DarkGray"
106
+ StrokeThickness="2" />
107
+ </Canvas>
108
+ </Grid>
109
+ </Window>
110
+ ```
111
+
112
+ ```cs
113
+ using System;
114
+ using System.Windows;
115
+ using System.Windows.Threading;
116
+ using Livet;
117
+
118
+ namespace Questions300617.ViewModels
119
+ {
120
+ public class MainWindowViewModel : ViewModel
121
+ {
122
+ private readonly Random random = new Random();
123
+ private DispatcherTimer timer;
124
+
125
+ private HorizontalAlignment _HorizontalAlignment;
126
+ public HorizontalAlignment HorizontalAlignment
127
+ {
128
+ get => _HorizontalAlignment;
129
+ set => RaisePropertyChangedIfSet(ref _HorizontalAlignment, value);
130
+ }
131
+
132
+ private VerticalAlignment _VerticalAlignment;
133
+ public VerticalAlignment VerticalAlignment
134
+ {
135
+ get => _VerticalAlignment;
136
+ set => RaisePropertyChangedIfSet(ref _VerticalAlignment, value);
137
+ }
138
+
139
+ public void Initialize()
140
+ {
141
+ timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1), };
142
+ timer.Tick += (s, e) =>
143
+ {
144
+ if(random.Next(2) == 0)
145
+ {
146
+ HorizontalAlignment = (HorizontalAlignment)random.Next(3);
147
+ }
148
+ else
149
+ {
150
+ VerticalAlignment = (VerticalAlignment)random.Next(3);
151
+ }
152
+ };
153
+ timer.Start();
154
+ }
155
+ }
156
+ }
157
+ ```
158
158
  長いのでボタンは省略しましたが、あってもOKです。