回答編集履歴

2

見直しキャンペーン中

2023/07/23 07:27

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,277 +1,139 @@
1
1
  `public Peaces peace = new Peaces();`
2
-
3
2
  は、
4
-
5
3
  `public Peaces Peace { get; } = new Peaces();`
6
-
7
4
  ですね。
8
-
9
5
  プロパティでないとバインドできないのと、xamlでは大文字になっています。
10
6
 
11
7
 
12
-
13
-
14
-
15
8
  `Source="{Binding Path=Peace[0][0]}"`
16
-
17
9
  は、
18
-
19
10
  `Source="{Binding Path=Peace[0\,0]}"`
20
-
21
11
  です。
22
-
23
12
  コードでは四角い配列([,])になっています。
24
-
25
13
  そのため`Peace[0,0]`となりますが、カンマがエラーになるので`\`でエスケープします。
26
-
27
-
28
-
29
14
 
30
15
 
31
16
  NaK310さんの制作物で使えるかどうかはわかりませんが、`ItemsControl`・`UniformGrid`でのxaml短縮化例も付けました。
32
17
 
33
-
34
-
35
18
  めんどくさいので2*2にしています^^;
36
19
 
37
-
38
-
39
- ```xaml
20
+ ```xml
40
-
41
21
  <Window
42
-
43
22
  x:Class="Questions296474.MainWindow"
44
-
45
23
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
46
-
47
24
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
48
-
49
25
  Width="500"
50
-
51
26
  Height="500">
52
-
53
27
  <StackPanel>
54
-
55
28
  <Button
56
-
57
29
  HorizontalAlignment="Center"
58
-
59
30
  Click="button_Click_START"
60
-
61
31
  Content="START"
62
-
63
32
  FontSize="20" />
64
-
65
33
  <Grid
66
-
67
34
  Width="120"
68
-
69
35
  Height="120"
70
-
71
36
  ShowGridLines="True">
72
-
73
37
  <Grid.ColumnDefinitions>
74
-
75
38
  <ColumnDefinition />
76
-
77
39
  <ColumnDefinition />
78
-
79
40
  </Grid.ColumnDefinitions>
80
-
81
41
  <Grid.RowDefinitions>
82
-
83
42
  <RowDefinition />
84
-
85
43
  <RowDefinition />
86
-
87
44
  </Grid.RowDefinitions>
88
-
89
45
  <Image Source="{Binding Path=Peace[0\,0]}" />
90
-
91
46
  <Image Grid.Column="1" Source="{Binding Path=Peace[1\,0]}" />
92
-
93
47
  <Image Grid.Row="1" Source="{Binding Path=Peace[0\,1]}" />
94
-
95
48
  <Image
96
-
97
49
  Grid.Row="1"
98
-
99
50
  Grid.Column="1"
100
-
101
51
  Source="{Binding Path=Peace[1\,1]}" />
102
-
103
52
  </Grid>
104
53
 
105
-
106
-
107
54
  <!-- ItemsControlでやった場合 XY反転しているので注意 -->
108
-
109
55
  <ItemsControl
110
-
111
56
  Width="120"
112
-
113
57
  Height="120"
114
-
115
58
  ItemsSource="{Binding Peace}">
116
-
117
59
  <ItemsControl.ItemsPanel>
118
-
119
60
  <ItemsPanelTemplate>
120
-
121
61
  <UniformGrid Columns="2" Rows="2" />
122
-
123
62
  </ItemsPanelTemplate>
124
-
125
63
  </ItemsControl.ItemsPanel>
126
-
127
64
  <ItemsControl.ItemTemplate>
128
-
129
65
  <DataTemplate>
130
-
131
66
  <Image Source="{Binding}" />
132
-
133
67
  </DataTemplate>
134
-
135
68
  </ItemsControl.ItemTemplate>
136
-
137
69
  </ItemsControl>
138
-
139
70
  </StackPanel>
140
-
141
71
  </Window>
142
-
143
72
  ```
144
73
 
145
-
146
-
147
- ```C#
74
+ ```cs
148
-
149
75
  using System;
150
-
151
76
  using System.Collections;
152
-
153
77
  using System.Collections.Generic;
154
-
155
78
  using System.Windows;
156
79
 
157
-
158
-
159
80
  namespace Questions296474
160
-
161
81
  {
162
-
163
82
  internal class Board
164
-
165
83
  {
166
-
167
84
  public const int WIDTH = 2;
168
-
169
85
  public const int HEIGHT = 2;
170
-
171
86
  public const string PEACE_A = @"https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg";
172
-
173
87
  public const string PEACE_B = @"https://teratail-v2.storage.googleapis.com/uploads/avatars/u14/143281/4935fb53e9921aff_thumbnail.jpg";
174
-
175
-
176
88
 
177
89
  public static readonly string[] PeaceType = new string[] { PEACE_A, PEACE_B };
178
90
 
179
-
180
-
181
91
  // プロパティでないとバインドできません
182
-
183
92
  public Peaces Peace { get; } = new Peaces();
184
93
 
185
-
186
-
187
94
  // 1回作ればいいです
188
-
189
95
  private static readonly Random rnd = new Random();
190
96
 
97
+ public Board()
98
+ {
99
+ for(var i = 0; i < HEIGHT; i++)
100
+ {
101
+ for(var j = 0; j < WIDTH; j++)
102
+ {
103
+ Peace[i, j] = PeaceType[rnd.Next(PeaceType.Length)];
104
+ }
105
+ }
106
+ }
107
+ }
191
108
 
109
+ internal class Peaces : IEnumerable<string>
110
+ {
111
+ public const int WIDTH = 2;
112
+ public const int HEIGHT = 2;
113
+ private readonly string[,] PeaceImg = new string[HEIGHT, WIDTH];
192
114
 
193
- public Board()
115
+ public string this[int X, int Y]
194
-
195
116
  {
196
-
197
- for(var i = 0; i < HEIGHT; i++)
198
-
199
- {
200
-
201
- for(var j = 0; j < WIDTH; j++)
117
+ set => PeaceImg[X, Y] = value;
202
-
203
- {
204
-
205
- Peace[i, j] = PeaceType[rnd.Next(PeaceType.Length)];
118
+ get => PeaceImg[X, Y];
206
-
207
- }
208
-
209
- }
210
-
211
119
  }
212
120
 
121
+ // ItemsControlで使えるようIEnumerable実装
122
+ public IEnumerator<string> GetEnumerator()
123
+ {
124
+ foreach(var item in PeaceImg) yield return item;
125
+ }
126
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
213
127
  }
214
128
 
215
-
216
-
217
- internal class Peaces : IEnumerable<string>
129
+ public partial class MainWindow : Window
218
-
219
130
  {
220
-
221
- public const int WIDTH = 2;
222
-
223
- public const int HEIGHT = 2;
224
-
225
- private readonly string[,] PeaceImg = new string[HEIGHT, WIDTH];
226
-
227
-
228
-
229
- public string this[int X, int Y]
230
-
231
- {
232
-
233
- set => PeaceImg[X, Y] = value;
234
-
235
- get => PeaceImg[X, Y];
236
-
237
- }
238
-
239
-
240
-
241
- // ItemsControlで使えるようIEnumerable実装
242
-
243
- public IEnumerator<string> GetEnumerator()
244
-
245
- {
246
-
247
- foreach(var item in PeaceImg) yield return item;
248
-
249
- }
250
-
251
- IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
252
-
253
- }
254
-
255
-
256
-
257
- public partial class MainWindow : Window
258
-
259
- {
260
-
261
131
  public MainWindow() => InitializeComponent();
262
132
 
263
-
264
-
265
133
  private void button_Click_START(object sender, RoutedEventArgs e)
266
-
267
134
  => DataContext = new Board();
268
-
269
135
  }
270
-
271
136
  }
272
-
273
137
  ```
274
138
 
275
-
276
-
277
139
  ![イメージ説明](f456f9147f9836f9dc9d90c0d3116009.png)

1

不要部分削除

2020/10/07 08:56

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -64,10 +64,6 @@
64
64
 
65
65
  <Grid
66
66
 
67
- x:Name="BoardGrid"
68
-
69
- Grid.Row="1"
70
-
71
67
  Width="120"
72
68
 
73
69
  Height="120"