回答編集履歴
2
見直しキャンペーン中
answer
CHANGED
@@ -1,139 +1,139 @@
|
|
1
|
-
`public Peaces peace = new Peaces();`
|
2
|
-
は、
|
3
|
-
`public Peaces Peace { get; } = new Peaces();`
|
4
|
-
ですね。
|
5
|
-
プロパティでないとバインドできないのと、xamlでは大文字になっています。
|
6
|
-
|
7
|
-
|
8
|
-
`Source="{Binding Path=Peace[0][0]}"`
|
9
|
-
は、
|
10
|
-
`Source="{Binding Path=Peace[0\,0]}"`
|
11
|
-
です。
|
12
|
-
コードでは四角い配列([,])になっています。
|
13
|
-
そのため`Peace[0,0]`となりますが、カンマがエラーになるので`\`でエスケープします。
|
14
|
-
|
15
|
-
|
16
|
-
NaK310さんの制作物で使えるかどうかはわかりませんが、`ItemsControl`・`UniformGrid`でのxaml短縮化例も付けました。
|
17
|
-
|
18
|
-
めんどくさいので2*2にしています^^;
|
19
|
-
|
20
|
-
```
|
21
|
-
<Window
|
22
|
-
x:Class="Questions296474.MainWindow"
|
23
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
24
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
25
|
-
Width="500"
|
26
|
-
Height="500">
|
27
|
-
<StackPanel>
|
28
|
-
<Button
|
29
|
-
HorizontalAlignment="Center"
|
30
|
-
Click="button_Click_START"
|
31
|
-
Content="START"
|
32
|
-
FontSize="20" />
|
33
|
-
<Grid
|
34
|
-
Width="120"
|
35
|
-
Height="120"
|
36
|
-
ShowGridLines="True">
|
37
|
-
<Grid.ColumnDefinitions>
|
38
|
-
<ColumnDefinition />
|
39
|
-
<ColumnDefinition />
|
40
|
-
</Grid.ColumnDefinitions>
|
41
|
-
<Grid.RowDefinitions>
|
42
|
-
<RowDefinition />
|
43
|
-
<RowDefinition />
|
44
|
-
</Grid.RowDefinitions>
|
45
|
-
<Image Source="{Binding Path=Peace[0\,0]}" />
|
46
|
-
<Image Grid.Column="1" Source="{Binding Path=Peace[1\,0]}" />
|
47
|
-
<Image Grid.Row="1" Source="{Binding Path=Peace[0\,1]}" />
|
48
|
-
<Image
|
49
|
-
Grid.Row="1"
|
50
|
-
Grid.Column="1"
|
51
|
-
Source="{Binding Path=Peace[1\,1]}" />
|
52
|
-
</Grid>
|
53
|
-
|
54
|
-
<!-- ItemsControlでやった場合 XY反転しているので注意 -->
|
55
|
-
<ItemsControl
|
56
|
-
Width="120"
|
57
|
-
Height="120"
|
58
|
-
ItemsSource="{Binding Peace}">
|
59
|
-
<ItemsControl.ItemsPanel>
|
60
|
-
<ItemsPanelTemplate>
|
61
|
-
<UniformGrid Columns="2" Rows="2" />
|
62
|
-
</ItemsPanelTemplate>
|
63
|
-
</ItemsControl.ItemsPanel>
|
64
|
-
<ItemsControl.ItemTemplate>
|
65
|
-
<DataTemplate>
|
66
|
-
<Image Source="{Binding}" />
|
67
|
-
</DataTemplate>
|
68
|
-
</ItemsControl.ItemTemplate>
|
69
|
-
</ItemsControl>
|
70
|
-
</StackPanel>
|
71
|
-
</Window>
|
72
|
-
```
|
73
|
-
|
74
|
-
```
|
75
|
-
using System;
|
76
|
-
using System.Collections;
|
77
|
-
using System.Collections.Generic;
|
78
|
-
using System.Windows;
|
79
|
-
|
80
|
-
namespace Questions296474
|
81
|
-
{
|
82
|
-
internal class Board
|
83
|
-
{
|
84
|
-
public const int WIDTH = 2;
|
85
|
-
public const int HEIGHT = 2;
|
86
|
-
public const string PEACE_A = @"https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg";
|
87
|
-
public const string PEACE_B = @"https://teratail-v2.storage.googleapis.com/uploads/avatars/u14/143281/4935fb53e9921aff_thumbnail.jpg";
|
88
|
-
|
89
|
-
public static readonly string[] PeaceType = new string[] { PEACE_A, PEACE_B };
|
90
|
-
|
91
|
-
// プロパティでないとバインドできません
|
92
|
-
public Peaces Peace { get; } = new Peaces();
|
93
|
-
|
94
|
-
// 1回作ればいいです
|
95
|
-
private static readonly Random rnd = new Random();
|
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
|
-
}
|
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];
|
114
|
-
|
115
|
-
public string this[int X, int Y]
|
116
|
-
{
|
117
|
-
set => PeaceImg[X, Y] = value;
|
118
|
-
get => PeaceImg[X, Y];
|
119
|
-
}
|
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();
|
127
|
-
}
|
128
|
-
|
129
|
-
public partial class MainWindow : Window
|
130
|
-
{
|
131
|
-
public MainWindow() => InitializeComponent();
|
132
|
-
|
133
|
-
private void button_Click_START(object sender, RoutedEventArgs e)
|
134
|
-
=> DataContext = new Board();
|
135
|
-
}
|
136
|
-
}
|
137
|
-
```
|
138
|
-
|
1
|
+
`public Peaces peace = new Peaces();`
|
2
|
+
は、
|
3
|
+
`public Peaces Peace { get; } = new Peaces();`
|
4
|
+
ですね。
|
5
|
+
プロパティでないとバインドできないのと、xamlでは大文字になっています。
|
6
|
+
|
7
|
+
|
8
|
+
`Source="{Binding Path=Peace[0][0]}"`
|
9
|
+
は、
|
10
|
+
`Source="{Binding Path=Peace[0\,0]}"`
|
11
|
+
です。
|
12
|
+
コードでは四角い配列([,])になっています。
|
13
|
+
そのため`Peace[0,0]`となりますが、カンマがエラーになるので`\`でエスケープします。
|
14
|
+
|
15
|
+
|
16
|
+
NaK310さんの制作物で使えるかどうかはわかりませんが、`ItemsControl`・`UniformGrid`でのxaml短縮化例も付けました。
|
17
|
+
|
18
|
+
めんどくさいので2*2にしています^^;
|
19
|
+
|
20
|
+
```xml
|
21
|
+
<Window
|
22
|
+
x:Class="Questions296474.MainWindow"
|
23
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
24
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
25
|
+
Width="500"
|
26
|
+
Height="500">
|
27
|
+
<StackPanel>
|
28
|
+
<Button
|
29
|
+
HorizontalAlignment="Center"
|
30
|
+
Click="button_Click_START"
|
31
|
+
Content="START"
|
32
|
+
FontSize="20" />
|
33
|
+
<Grid
|
34
|
+
Width="120"
|
35
|
+
Height="120"
|
36
|
+
ShowGridLines="True">
|
37
|
+
<Grid.ColumnDefinitions>
|
38
|
+
<ColumnDefinition />
|
39
|
+
<ColumnDefinition />
|
40
|
+
</Grid.ColumnDefinitions>
|
41
|
+
<Grid.RowDefinitions>
|
42
|
+
<RowDefinition />
|
43
|
+
<RowDefinition />
|
44
|
+
</Grid.RowDefinitions>
|
45
|
+
<Image Source="{Binding Path=Peace[0\,0]}" />
|
46
|
+
<Image Grid.Column="1" Source="{Binding Path=Peace[1\,0]}" />
|
47
|
+
<Image Grid.Row="1" Source="{Binding Path=Peace[0\,1]}" />
|
48
|
+
<Image
|
49
|
+
Grid.Row="1"
|
50
|
+
Grid.Column="1"
|
51
|
+
Source="{Binding Path=Peace[1\,1]}" />
|
52
|
+
</Grid>
|
53
|
+
|
54
|
+
<!-- ItemsControlでやった場合 XY反転しているので注意 -->
|
55
|
+
<ItemsControl
|
56
|
+
Width="120"
|
57
|
+
Height="120"
|
58
|
+
ItemsSource="{Binding Peace}">
|
59
|
+
<ItemsControl.ItemsPanel>
|
60
|
+
<ItemsPanelTemplate>
|
61
|
+
<UniformGrid Columns="2" Rows="2" />
|
62
|
+
</ItemsPanelTemplate>
|
63
|
+
</ItemsControl.ItemsPanel>
|
64
|
+
<ItemsControl.ItemTemplate>
|
65
|
+
<DataTemplate>
|
66
|
+
<Image Source="{Binding}" />
|
67
|
+
</DataTemplate>
|
68
|
+
</ItemsControl.ItemTemplate>
|
69
|
+
</ItemsControl>
|
70
|
+
</StackPanel>
|
71
|
+
</Window>
|
72
|
+
```
|
73
|
+
|
74
|
+
```cs
|
75
|
+
using System;
|
76
|
+
using System.Collections;
|
77
|
+
using System.Collections.Generic;
|
78
|
+
using System.Windows;
|
79
|
+
|
80
|
+
namespace Questions296474
|
81
|
+
{
|
82
|
+
internal class Board
|
83
|
+
{
|
84
|
+
public const int WIDTH = 2;
|
85
|
+
public const int HEIGHT = 2;
|
86
|
+
public const string PEACE_A = @"https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg";
|
87
|
+
public const string PEACE_B = @"https://teratail-v2.storage.googleapis.com/uploads/avatars/u14/143281/4935fb53e9921aff_thumbnail.jpg";
|
88
|
+
|
89
|
+
public static readonly string[] PeaceType = new string[] { PEACE_A, PEACE_B };
|
90
|
+
|
91
|
+
// プロパティでないとバインドできません
|
92
|
+
public Peaces Peace { get; } = new Peaces();
|
93
|
+
|
94
|
+
// 1回作ればいいです
|
95
|
+
private static readonly Random rnd = new Random();
|
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
|
+
}
|
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];
|
114
|
+
|
115
|
+
public string this[int X, int Y]
|
116
|
+
{
|
117
|
+
set => PeaceImg[X, Y] = value;
|
118
|
+
get => PeaceImg[X, Y];
|
119
|
+
}
|
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();
|
127
|
+
}
|
128
|
+
|
129
|
+
public partial class MainWindow : Window
|
130
|
+
{
|
131
|
+
public MainWindow() => InitializeComponent();
|
132
|
+
|
133
|
+
private void button_Click_START(object sender, RoutedEventArgs e)
|
134
|
+
=> DataContext = new Board();
|
135
|
+
}
|
136
|
+
}
|
137
|
+
```
|
138
|
+
|
139
139
|

|
1
不要部分削除
answer
CHANGED
@@ -31,8 +31,6 @@
|
|
31
31
|
Content="START"
|
32
32
|
FontSize="20" />
|
33
33
|
<Grid
|
34
|
-
x:Name="BoardGrid"
|
35
|
-
Grid.Row="1"
|
36
34
|
Width="120"
|
37
35
|
Height="120"
|
38
36
|
ShowGridLines="True">
|