回答編集履歴

1

見直しキャンペーン中

2023/07/29 09:54

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,283 +1,146 @@
1
1
  > ソースコード中に、手計算で算出した数値がいくつかありますが、それらは自動的に算出させて、手計算を不要にしたいです。
2
2
 
3
-
4
-
5
3
  よくあるのは`DoubleAnimation`は、0~1の比率としてアニメーションさせる手法です(そのかわり`Converter`が必要になります)
6
-
7
4
  [WPF animation: binding to the "To" attribute of storyboard animation - Stack Overflow](https://stackoverflow.com/questions/2186933/wpf-animation-binding-to-the-to-attribute-of-storyboard-animation/14164245#14164245)
8
-
9
-
10
5
 
11
6
  比率でやったとしても基準になる数値は必要になりますが、お手軽なのはダミーで実際に作ってしまうことです(`Collapsed`にしてしまうとサイズ0になってしまうので、`Hidden`にしたり何かの裏に隠します^^;
12
7
 
13
-
14
-
15
- ```xaml
8
+ ```xml
16
-
17
9
  <Window
18
-
19
10
  x:Class="Questions371156.MainWindow"
20
-
21
11
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
-
23
12
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
-
25
13
  xmlns:local="clr-namespace:Questions371156"
26
-
27
14
  Width="800"
28
-
29
15
  Height="450">
30
-
31
16
  <Window.Resources>
32
-
33
17
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
34
-
35
18
  <local:MultiplyConverter x:Key="multiplyConverter" />
36
-
37
19
  </Window.Resources>
38
-
39
20
  <DockPanel>
40
-
41
21
  <StackPanel Background="DarkGray">
42
-
43
22
  <StackPanel.Width>
44
-
45
23
  <!-- 比率が0~1にアニメーションすると、StackPanel.Widthが53~145に変化する(ようなコンバータを書く) -->
46
-
47
24
  <MultiBinding Converter="{StaticResource multiplyConverter}">
48
-
49
25
  <!-- 初期値: 53の部分 -->
50
-
51
26
  <Binding ElementName="dummy_min_button" Path="ActualWidth" />
52
-
53
27
  <!-- ターゲット値: 145の部分 -->
54
-
55
28
  <Binding ElementName="side_menu_panel2" Path="DesiredSize.Width" />
56
-
57
29
  <!-- 比率: 0~1で変化する -->
58
-
59
30
  <Binding ElementName="dummy_width_ratio" Path="Width" />
60
-
61
31
  </MultiBinding>
62
-
63
32
  </StackPanel.Width>
64
33
 
65
-
66
-
67
34
  <StackPanel.Triggers>
68
-
69
35
  <EventTrigger RoutedEvent="MouseEnter" SourceName="side_menu_panel2">
70
-
71
36
  <BeginStoryboard>
72
-
73
37
  <Storyboard>
74
-
75
38
  <DoubleAnimation
76
-
77
39
  Storyboard.TargetName="dummy_width_ratio"
78
-
79
40
  Storyboard.TargetProperty="Width"
80
-
81
41
  To="1"
82
-
83
42
  Duration="0:0:0.3">
84
-
85
43
  <DoubleAnimation.EasingFunction>
86
-
87
44
  <CubicEase />
88
-
89
45
  </DoubleAnimation.EasingFunction>
90
-
91
46
  </DoubleAnimation>
92
-
93
47
  </Storyboard>
94
-
95
48
  </BeginStoryboard>
96
-
97
49
  </EventTrigger>
98
-
99
50
  <EventTrigger RoutedEvent="MouseLeave" SourceName="side_menu_panel2">
100
-
101
51
  <BeginStoryboard>
102
-
103
52
  <Storyboard>
104
-
105
53
  <DoubleAnimation
106
-
107
54
  Storyboard.TargetName="dummy_width_ratio"
108
-
109
55
  Storyboard.TargetProperty="Width"
110
-
111
56
  To="0"
112
-
113
57
  Duration="0:0:0.15">
114
-
115
58
  <DoubleAnimation.EasingFunction>
116
-
117
59
  <CubicEase />
118
-
119
60
  </DoubleAnimation.EasingFunction>
120
-
121
61
  </DoubleAnimation>
122
-
123
62
  </Storyboard>
124
-
125
63
  </BeginStoryboard>
126
-
127
64
  </EventTrigger>
128
-
129
65
  </StackPanel.Triggers>
130
-
131
66
  <StackPanel.Resources>
132
-
133
67
  <Style TargetType="RadioButton">
134
-
135
68
  <Setter Property="Padding" Value="4" />
136
-
137
69
  <Setter Property="VerticalContentAlignment" Value="Center" />
138
-
139
70
  <Setter Property="Template">
140
-
141
71
  <Setter.Value>
142
-
143
72
  <ControlTemplate TargetType="RadioButton">
144
-
73
+ <Border
145
- <Border Padding="{TemplateBinding Padding}" Cursor="Hand">
74
+ Padding="{TemplateBinding Padding}"
146
-
75
+ Background="Transparent"
76
+ Cursor="Hand">
147
77
  <BulletDecorator>
148
-
149
78
  <BulletDecorator.Bullet>
150
-
151
79
  <!-- Background使ってなさそうなのでこっちで使いました^^; -->
152
-
153
80
  <Ellipse
154
-
155
81
  Width="45"
156
-
157
82
  Height="45"
158
-
159
83
  Fill="{TemplateBinding Background}" />
160
-
161
84
  </BulletDecorator.Bullet>
162
-
163
85
  <ContentPresenter
164
-
165
86
  x:Name="contentPresenter"
166
-
167
87
  Margin="20,0,0,0"
168
-
169
88
  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
170
-
171
89
  Visibility="{TemplateBinding HasContent, Converter={StaticResource BooleanToVisibilityConverter}}" />
172
-
173
90
  </BulletDecorator>
174
-
175
91
  </Border>
176
-
177
92
  </ControlTemplate>
178
-
179
93
  </Setter.Value>
180
-
181
94
  </Setter>
182
-
183
95
  </Style>
184
-
185
96
  </StackPanel.Resources>
186
-
187
97
  <!-- ダミーを隠すためにGridで重なってもらう -->
188
-
189
98
  <Grid>
190
-
191
99
  <!-- 閉じたときのサイズ計算用ダミー -->
192
-
193
100
  <RadioButton x:Name="dummy_min_button" HorizontalAlignment="Left" />
194
-
195
101
  <!-- 比率計算用ダミー -->
196
-
197
102
  <Border x:Name="dummy_width_ratio" Width="0" />
198
103
 
199
-
200
-
201
104
  <StackPanel x:Name="side_menu_panel2">
202
-
203
105
  <RadioButton Background="#FF21E402" Content="メニュー1" />
204
-
205
106
  <RadioButton Background="MediumVioletRed" Content="メニューーー2" />
206
-
207
107
  </StackPanel>
208
-
209
108
  </Grid>
210
-
211
109
  </StackPanel>
212
-
213
110
  <Rectangle Fill="Gray" />
214
-
215
111
  </DockPanel>
216
-
217
112
  </Window>
218
-
219
113
  ```
220
114
 
221
-
222
-
223
- ```C#
115
+ ```cs
224
-
225
116
  using System;
226
-
227
117
  using System.Globalization;
228
-
229
118
  using System.Windows;
230
-
231
119
  using System.Windows.Data;
232
120
 
233
-
234
-
235
121
  namespace Questions371156
236
-
237
122
  {
238
-
239
123
  public class MultiplyConverter : IMultiValueConverter
240
-
241
124
  {
242
-
243
125
  // values[0]:初期値 values[1]:ターゲット値 values[2]:比率 が入力される前提
244
-
245
126
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
246
-
247
127
  => (double)values[0] + (double)values[2] * ((double)values[1] - (double)values[0]);
248
-
249
128
 
250
-
251
129
  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
252
-
253
130
  }
254
131
 
255
-
256
-
257
132
  public partial class MainWindow : Window
258
-
259
133
  {
260
-
261
134
  public MainWindow() => InitializeComponent();
262
-
263
135
  }
264
-
265
136
  }
266
-
267
137
  ```
268
-
138
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/f2ba8ea8-e660-47e4-9cc5-c7ffc2f8fd8f.gif)
269
-
270
139
 
271
140
  ---
272
141
 
273
142
 
274
-
275
-
276
-
277
143
  アニメーションは「引っ込むときは速くする・イージングをつける」と、オサレな感じになると思います^^
278
144
 
279
-
280
-
281
145
  WPF用ではありませんがUXのガイドラインがあります。
282
-
283
146
  [モーション (Windows アプリでのアニメーション) - Windows apps | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/apps/design/motion/motion-in-practice)