回答編集履歴

1

見直しキャンペーン中

2023/07/29 10:35

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,231 +1,116 @@
1
1
  `image_[x] = new Image();`こうしたところで、元の`ButtonImage_0`等には何の影響もありません。
2
-
3
-
4
2
 
5
3
  `Image`はすでにxamlで配置しているわけですから新たに作る必要はなく、`Source`を変更するだけでいいのです^^
6
4
 
7
5
 
8
-
9
-
10
-
11
- ```xaml
6
+ ```xml
12
-
13
7
  <Window
14
-
15
8
  x:Class="Questions372108.MainWindow"
16
-
17
9
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
18
-
19
10
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
20
-
21
11
  Width="800"
22
-
23
12
  Height="450">
24
-
25
13
  <StackPanel>
14
+ <UniformGrid Rows="1">
15
+ <Button x:Name="Button_0" Click="Button_Click" Content="Button_0" />
16
+ <Button x:Name="Button_1" Click="Button_Click" Content="Button_1" />
17
+ <Button x:Name="Button_2" Click="Button_Click" Content="Button_2" />
18
+ <Button x:Name="Button_3" Click="Button_Click" Content="Button_3" />
19
+ <Button x:Name="Button_4" Click="Button_Click" Content="Button_4" />
20
+ </UniformGrid>
26
21
 
27
22
  <UniformGrid Rows="1">
28
-
29
- <Button x:Name="Button_0" Click="Button_Click" Content="Button_0" />
23
+ <Image x:Name="ButtonImage_0" IsHitTestVisible="False" />
30
-
31
- <Button x:Name="Button_1" Click="Button_Click" Content="Button_1" />
24
+ <Image x:Name="ButtonImage_1" IsHitTestVisible="False" />
32
-
33
- <Button x:Name="Button_2" Click="Button_Click" Content="Button_2" />
25
+ <Image x:Name="ButtonImage_2" IsHitTestVisible="False" />
34
-
35
- <Button x:Name="Button_3" Click="Button_Click" Content="Button_3" />
26
+ <Image x:Name="ButtonImage_3" IsHitTestVisible="False" />
36
-
37
- <Button x:Name="Button_4" Click="Button_Click" Content="Button_4" />
27
+ <Image x:Name="ButtonImage_4" IsHitTestVisible="False" />
38
-
39
28
  </UniformGrid>
40
-
41
-
42
-
43
- <UniformGrid Rows="1">
44
-
45
- <Image x:Name="ButtonImage_0" IsHitTestVisible="False" />
46
-
47
- <Image x:Name="ButtonImage_1" IsHitTestVisible="False" />
48
-
49
- <Image x:Name="ButtonImage_2" IsHitTestVisible="False" />
50
-
51
- <Image x:Name="ButtonImage_3" IsHitTestVisible="False" />
52
-
53
- <Image x:Name="ButtonImage_4" IsHitTestVisible="False" />
54
-
55
- </UniformGrid>
56
-
57
29
  </StackPanel>
58
-
59
30
  </Window>
60
-
61
31
  ```
62
32
 
63
-
64
-
65
- ```C#
33
+ ```cs
66
-
67
34
  using System;
68
-
69
35
  using System.Windows;
70
-
71
36
  using System.Windows.Controls;
72
-
73
37
  using System.Windows.Media.Imaging;
74
38
 
75
-
76
-
77
39
  namespace Questions372108
78
-
79
40
  {
80
-
81
41
  public partial class MainWindow : Window
82
-
83
42
  {
84
-
85
43
  private readonly int[] counts = new int[5];
86
-
87
44
  private readonly Button[] buttons = new Button[5];
88
-
89
45
  private readonly Image[] images = new Image[5];
90
46
 
91
-
92
-
93
47
  private Button Button_0_Test;
94
-
95
48
  private Button currentButton;
96
49
 
97
-
98
-
99
50
  public MainWindow()
100
-
101
51
  {
102
-
103
52
  InitializeComponent();
104
53
 
54
+ for (var i = 0; i < 5; i++)
55
+ {
56
+ buttons[i] = FindName($"Button_{i}") as Button;
57
+ images[i] = FindName($"ButtonImage_{i}") as Image;
58
+ }
105
59
 
60
+ Button_0_Test = Button_0;
61
+ }
106
62
 
63
+ private void Button_Click(object sender, RoutedEventArgs e)
64
+ {
107
- for (var i = 0; i < 5; i++)
65
+ var x = Array.IndexOf(buttons, sender);
108
66
 
67
+ if (counts[x] == 0)
109
68
  {
69
+ var bi = new BitmapImage();
70
+ bi.BeginInit();
71
+ //bi.UriSource = new Uri("/Resources/AAA.png", UriKind.Relative);
72
+ bi.UriSource = new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg");
73
+ bi.EndInit();
110
74
 
75
+ images[x].Source = bi;
76
+ counts[x]++;
77
+ }
78
+ else if (counts[x] == 1)
79
+ {
80
+ var bi = new BitmapImage();
81
+ bi.BeginInit();
111
- buttons[i] = FindName($"Button_{i}") as Button;
82
+ //bi.UriSource = new Uri("/Resources/BBB.png", UriKind.Relative);
83
+ bi.UriSource = new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u20/201856/m2cobCxI_thumbnail.jpg");
84
+ bi.EndInit();
112
85
 
86
+ images[x].Source = bi;
87
+ counts[x]++;
88
+ }
89
+ else if (counts[x] == 2)
90
+ {
113
- images[i] = FindName($"ButtonImage_{i}") as Image;
91
+ images[x].Source = null;
114
-
92
+ counts[x] = 0;
115
93
  }
116
94
 
117
95
 
118
-
119
- Button_0_Test = Button_0;
120
-
121
- }
122
-
123
-
124
-
125
- private void Button_Click(object sender, RoutedEventArgs e)
126
-
127
- {
128
-
129
- var x = Array.IndexOf(buttons, sender);
130
-
131
-
132
-
133
- if (counts[x] == 0)
134
-
135
- {
136
-
137
- var bi = new BitmapImage();
138
-
139
- bi.BeginInit();
140
-
141
- //bi.UriSource = new Uri("/Resources/AAA.png", UriKind.Relative);
142
-
143
- bi.UriSource = new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg");
144
-
145
- bi.EndInit();
146
-
147
-
148
-
149
- images[x].Source = bi;
150
-
151
- counts[x]++;
152
-
153
- }
154
-
155
- else if (counts[x] == 1)
156
-
157
- {
158
-
159
- var bi = new BitmapImage();
160
-
161
- bi.BeginInit();
162
-
163
- //bi.UriSource = new Uri("/Resources/BBB.png", UriKind.Relative);
164
-
165
- bi.UriSource = new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u20/201856/m2cobCxI_thumbnail.jpg");
166
-
167
- bi.EndInit();
168
-
169
-
170
-
171
- images[x].Source = bi;
172
-
173
- counts[x]++;
174
-
175
- }
176
-
177
- else if (counts[x] == 2)
178
-
179
- {
180
-
181
- images[x].Source = null;
182
-
183
- counts[x] = 0;
184
-
185
- }
186
-
187
-
188
-
189
-
190
-
191
96
  // これが何の意味もない理由
192
-
193
97
  //image_[x] = new Image();
194
98
 
195
-
196
-
197
99
  // 例えばこうしたとして、表示されているButton_0が入れ替わるわけではない!
198
-
199
100
  Button_0_Test = new Button { Content = "Button_0がこうは変わらない!", };
200
101
 
201
-
202
-
203
102
  // 単に変数Button_0_Testの指す先が、新しいボタンに変わっただけ
204
-
205
103
  // Button_0は依然表示されたまま
206
-
207
104
  // 新たに作ったボタンはウィンドウに追加されていないので、
208
-
209
105
  // 表示されずにただメモリ内にあるだけになっている
210
106
 
211
107
 
212
-
213
-
214
-
215
108
  // こういった使い方なら意味はあるが
216
-
217
109
  currentButton = sender as Button;
218
-
219
110
  currentButton.Content = $"Button_{x}_{counts[x]}";
220
-
221
111
  //buttons[x].Content = $"Button_{x}_{counts[x]}";
222
-
223
112
  }
224
-
225
113
  }
226
-
227
114
  }
228
-
229
115
  ```
230
-
231
116
  ![アプリ画像](aebe654b5e9dc7d1809fcd7a84d35622.png)