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

回答編集履歴

1

見直しキャンペーン中

2023/07/29 10:35

投稿

TN8001
TN8001

スコア10180

answer CHANGED
@@ -1,116 +1,116 @@
1
- `image_[x] = new Image();`こうしたところで、元の`ButtonImage_0`等には何の影響もありません。
2
-
3
- `Image`はすでにxamlで配置しているわけですから新たに作る必要はなく、`Source`を変更するだけでいいのです^^
4
-
5
-
6
- ```xaml
7
- <Window
8
- x:Class="Questions372108.MainWindow"
9
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
- Width="800"
12
- Height="450">
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>
21
-
22
- <UniformGrid Rows="1">
23
- <Image x:Name="ButtonImage_0" IsHitTestVisible="False" />
24
- <Image x:Name="ButtonImage_1" IsHitTestVisible="False" />
25
- <Image x:Name="ButtonImage_2" IsHitTestVisible="False" />
26
- <Image x:Name="ButtonImage_3" IsHitTestVisible="False" />
27
- <Image x:Name="ButtonImage_4" IsHitTestVisible="False" />
28
- </UniformGrid>
29
- </StackPanel>
30
- </Window>
31
- ```
32
-
33
- ```C#
34
- using System;
35
- using System.Windows;
36
- using System.Windows.Controls;
37
- using System.Windows.Media.Imaging;
38
-
39
- namespace Questions372108
40
- {
41
- public partial class MainWindow : Window
42
- {
43
- private readonly int[] counts = new int[5];
44
- private readonly Button[] buttons = new Button[5];
45
- private readonly Image[] images = new Image[5];
46
-
47
- private Button Button_0_Test;
48
- private Button currentButton;
49
-
50
- public MainWindow()
51
- {
52
- InitializeComponent();
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
- }
59
-
60
- Button_0_Test = Button_0;
61
- }
62
-
63
- private void Button_Click(object sender, RoutedEventArgs e)
64
- {
65
- var x = Array.IndexOf(buttons, sender);
66
-
67
- if (counts[x] == 0)
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();
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();
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();
85
-
86
- images[x].Source = bi;
87
- counts[x]++;
88
- }
89
- else if (counts[x] == 2)
90
- {
91
- images[x].Source = null;
92
- counts[x] = 0;
93
- }
94
-
95
-
96
- // これが何の意味もない理由
97
- //image_[x] = new Image();
98
-
99
- // 例えばこうしたとして、表示されているButton_0が入れ替わるわけではない!
100
- Button_0_Test = new Button { Content = "Button_0がこうは変わらない!", };
101
-
102
- // 単に変数Button_0_Testの指す先が、新しいボタンに変わっただけ
103
- // Button_0は依然表示されたまま
104
- // 新たに作ったボタンはウィンドウに追加されていないので、
105
- // 表示されずにただメモリ内にあるだけになっている
106
-
107
-
108
- // こういった使い方なら意味はあるが
109
- currentButton = sender as Button;
110
- currentButton.Content = $"Button_{x}_{counts[x]}";
111
- //buttons[x].Content = $"Button_{x}_{counts[x]}";
112
- }
113
- }
114
- }
115
- ```
1
+ `image_[x] = new Image();`こうしたところで、元の`ButtonImage_0`等には何の影響もありません。
2
+
3
+ `Image`はすでにxamlで配置しているわけですから新たに作る必要はなく、`Source`を変更するだけでいいのです^^
4
+
5
+
6
+ ```xml
7
+ <Window
8
+ x:Class="Questions372108.MainWindow"
9
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
+ Width="800"
12
+ Height="450">
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>
21
+
22
+ <UniformGrid Rows="1">
23
+ <Image x:Name="ButtonImage_0" IsHitTestVisible="False" />
24
+ <Image x:Name="ButtonImage_1" IsHitTestVisible="False" />
25
+ <Image x:Name="ButtonImage_2" IsHitTestVisible="False" />
26
+ <Image x:Name="ButtonImage_3" IsHitTestVisible="False" />
27
+ <Image x:Name="ButtonImage_4" IsHitTestVisible="False" />
28
+ </UniformGrid>
29
+ </StackPanel>
30
+ </Window>
31
+ ```
32
+
33
+ ```cs
34
+ using System;
35
+ using System.Windows;
36
+ using System.Windows.Controls;
37
+ using System.Windows.Media.Imaging;
38
+
39
+ namespace Questions372108
40
+ {
41
+ public partial class MainWindow : Window
42
+ {
43
+ private readonly int[] counts = new int[5];
44
+ private readonly Button[] buttons = new Button[5];
45
+ private readonly Image[] images = new Image[5];
46
+
47
+ private Button Button_0_Test;
48
+ private Button currentButton;
49
+
50
+ public MainWindow()
51
+ {
52
+ InitializeComponent();
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
+ }
59
+
60
+ Button_0_Test = Button_0;
61
+ }
62
+
63
+ private void Button_Click(object sender, RoutedEventArgs e)
64
+ {
65
+ var x = Array.IndexOf(buttons, sender);
66
+
67
+ if (counts[x] == 0)
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();
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();
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();
85
+
86
+ images[x].Source = bi;
87
+ counts[x]++;
88
+ }
89
+ else if (counts[x] == 2)
90
+ {
91
+ images[x].Source = null;
92
+ counts[x] = 0;
93
+ }
94
+
95
+
96
+ // これが何の意味もない理由
97
+ //image_[x] = new Image();
98
+
99
+ // 例えばこうしたとして、表示されているButton_0が入れ替わるわけではない!
100
+ Button_0_Test = new Button { Content = "Button_0がこうは変わらない!", };
101
+
102
+ // 単に変数Button_0_Testの指す先が、新しいボタンに変わっただけ
103
+ // Button_0は依然表示されたまま
104
+ // 新たに作ったボタンはウィンドウに追加されていないので、
105
+ // 表示されずにただメモリ内にあるだけになっている
106
+
107
+
108
+ // こういった使い方なら意味はあるが
109
+ currentButton = sender as Button;
110
+ currentButton.Content = $"Button_{x}_{counts[x]}";
111
+ //buttons[x].Content = $"Button_{x}_{counts[x]}";
112
+ }
113
+ }
114
+ }
115
+ ```
116
116
  ![アプリ画像](aebe654b5e9dc7d1809fcd7a84d35622.png)