回答編集履歴

1

見直しキャンペーン中

2023/07/23 08:38

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,241 +1,121 @@
1
1
  `RenderTransform`が`One`にしか付いていないので、ほかの目が出たときにエラーになります。
2
-
3
2
  全部に付ければ想定通りに動きます。
4
3
 
5
-
6
-
7
- ```xaml
4
+ ```xml
8
-
9
5
  <Window
10
-
11
6
  x:Class="Questions300589.MainWindow"
12
-
13
7
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
-
15
8
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
-
17
9
  Width="300"
18
-
19
10
  Height="450">
20
-
21
11
  <Window.Resources>
22
-
23
12
  <Storyboard x:Key="Jump">
24
-
25
13
  <DoubleAnimationUsingKeyFrames Storyboard.TargetName="{Binding DEME_NAME}" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
26
-
27
14
  <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-20" />
28
-
29
15
  <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
30
-
31
16
  </DoubleAnimationUsingKeyFrames>
32
-
33
17
  </Storyboard>
34
18
 
35
-
36
-
37
19
  <Style TargetType="{x:Type TextBlock}">
38
-
39
20
  <Setter Property="HorizontalAlignment" Value="Center" />
40
-
41
21
  <Setter Property="VerticalAlignment" Value="Center" />
42
-
43
22
  <Setter Property="FontSize" Value="36" />
44
-
45
23
  <Setter Property="RenderTransform">
46
-
47
24
  <Setter.Value>
48
-
49
25
  <TranslateTransform />
50
-
51
26
  </Setter.Value>
52
-
53
27
  </Setter>
54
-
55
28
  </Style>
56
-
57
29
  </Window.Resources>
58
-
59
30
  <Grid>
60
-
61
31
  <Grid.RowDefinitions>
62
-
63
32
  <RowDefinition />
64
-
65
33
  <RowDefinition />
66
-
67
34
  <RowDefinition Height="3*" />
68
-
69
35
  </Grid.RowDefinitions>
70
-
71
36
  <Button
72
-
73
37
  HorizontalAlignment="Center"
74
-
75
38
  VerticalAlignment="Center"
76
-
77
39
  Click="Button_Click"
78
-
79
40
  Content="さいころを振る" />
80
-
81
-
82
41
 
83
42
  <TextBlock FontSize="24" Text="{Binding DEME}" Grid.Row="1" />
84
43
 
85
-
86
-
87
44
  <UniformGrid Grid.Row="2" Columns="2">
88
-
89
45
  <TextBlock x:Name="One" Text="1" />
90
-
91
46
  <TextBlock x:Name="Two" Text="2" />
92
-
93
47
  <TextBlock x:Name="Three" Text="3" />
94
-
95
48
  <TextBlock x:Name="Four" Text="4" />
96
-
97
49
  <TextBlock x:Name="Five" Text="5" />
98
-
99
50
  <TextBlock x:Name="Six" Text="6" />
100
-
101
51
  </UniformGrid>
102
-
103
52
  </Grid>
104
-
105
53
  </Window>
106
-
107
54
  ```
108
55
 
109
-
110
-
111
- ```C#
56
+ ```cs
112
-
113
57
  using System;
114
-
115
58
  using System.ComponentModel;
116
-
117
59
  using System.Windows;
118
-
119
60
  using System.Windows.Media.Animation;
120
61
 
121
-
122
-
123
62
  namespace Questions300589
124
-
125
63
  {
126
-
127
64
  public partial class MainWindow : Window
128
-
129
65
  {
130
-
131
66
  private readonly Hoge hoge = new Hoge();
132
-
133
67
  private readonly Storyboard sb;
134
68
 
135
-
136
-
137
69
  public MainWindow()
138
-
139
70
  {
140
-
141
71
  InitializeComponent();
142
-
143
72
  DataContext = hoge;
144
-
145
73
  sb = FindResource("Jump") as Storyboard;
146
-
147
74
  }
148
75
 
149
-
150
-
151
76
  private void Button_Click(object sender, RoutedEventArgs e)
152
-
153
77
  {
154
-
155
78
  hoge.Shake();
156
-
157
79
  BeginStoryboard(sb);
158
-
159
80
  }
160
-
161
81
  }
162
82
 
163
-
164
-
165
83
  public class Hoge : INotifyPropertyChanged
166
-
167
84
  {
168
-
169
85
  private static readonly Random rnd = new Random();
170
-
171
-
172
86
 
173
87
  public int DEME { get; private set; }
174
88
 
175
-
176
-
177
89
  public string DEME_NAME
178
-
179
90
  {
180
-
181
91
  get
182
-
183
92
  {
184
-
185
93
  switch(DEME)
186
-
187
94
  {
188
-
189
95
  default:
190
-
191
96
  case 1: return "One";
192
-
193
97
  case 2: return "Two";
194
-
195
98
  case 3: return "Three";
196
-
197
99
  case 4: return "Four";
198
-
199
100
  case 5: return "Five";
200
-
201
101
  case 6: return "Six";
202
-
203
102
  }
204
-
205
103
  }
206
-
207
104
  }
208
105
 
209
-
210
-
211
106
  public void Shake()
212
-
213
107
  {
214
-
215
108
  DEME = rnd.Next(1, 7);
216
-
217
109
  RaiseProeprtyChanged(nameof(DEME));
218
-
219
110
  RaiseProeprtyChanged(nameof(DEME_NAME));
220
-
221
111
  }
222
112
 
223
-
224
-
225
113
  #region INotifyPropertyChanged の実装
226
-
227
114
  public event PropertyChangedEventHandler PropertyChanged;
228
-
229
115
  private void RaiseProeprtyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
230
-
231
116
  #endregion
232
-
233
117
  }
234
-
235
118
  }
236
-
237
119
  ```
238
120
 
239
-
240
-
241
121
  xamlが長くなりそうだったのでだいぶ変えてしまいましたが、元の形でもOKです。