回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,78 +1,78 @@
|
|
1
|
-
> DataGridのMVVM形式での実装は下記を参考にしました
|
2
|
-
> https://www.fenet.jp/dotnet/column/tool/4287/
|
3
|
-
|
4
|
-
ほんとうですか!?似ても似つかないように思うのですが。。
|
5
|
-
|
6
|
-
コレクションと、個々のアイテムの区別がついていないように見受けられます。
|
7
|
-
|
8
|
-
コレクションとは、`ObservableCollection`や`List`・配列等複数のアイテムを持つものです(回答コードでいえば`People`)
|
9
|
-
|
10
|
-
個々のアイテムとは、コレクションの中のひとつひとつです(回答コードでいえば`Person`)
|
11
|
-
|
12
|
-
`DataGrid`の`ItemsSource`にはコレクションをバインドします。
|
13
|
-
|
14
|
-
`DataGridTextColumn`の段階では`DataContext`は個々のアイテムになっているので、`{Binding ParamA.ID}`ではなく`{Binding ID}`とプロパティだけでよいことになります。
|
15
|
-
|
16
|
-
```
|
17
|
-
<Window
|
18
|
-
x:Class="Questions353106.MainWindow"
|
19
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
-
xmlns:local="clr-namespace:Questions353106"
|
22
|
-
Width="800"
|
23
|
-
Height="450">
|
24
|
-
<Window.DataContext>
|
25
|
-
<local:MainWindowVieModel />
|
26
|
-
</Window.DataContext>
|
27
|
-
<Grid>
|
28
|
-
<DataGrid
|
29
|
-
AutoGenerateColumns="False"
|
30
|
-
IsReadOnly="True"
|
31
|
-
ItemsSource="{Binding People}">
|
32
|
-
<DataGrid.Columns>
|
33
|
-
<DataGridTextColumn Binding="{Binding ID}" Header="ID" />
|
34
|
-
<DataGridTextColumn Binding="{Binding Name}" Header="Name" />
|
35
|
-
<DataGridTextColumn Binding="{Binding Sex}" Header="Sex" />
|
36
|
-
<DataGridTextColumn Binding="{Binding Email}" Header="メール" />
|
37
|
-
</DataGrid.Columns>
|
38
|
-
</DataGrid>
|
39
|
-
</Grid>
|
40
|
-
</Window>
|
41
|
-
```
|
42
|
-
|
43
|
-
```
|
44
|
-
using System.Collections.ObjectModel;
|
45
|
-
using System.Windows;
|
46
|
-
|
47
|
-
namespace Questions353106
|
48
|
-
{
|
49
|
-
class Person
|
50
|
-
{
|
51
|
-
public int ID { get; set; }
|
52
|
-
public string Name { get; set; }
|
53
|
-
public string Sex { get; set; }
|
54
|
-
public string Email { get; set; }
|
55
|
-
}
|
56
|
-
|
57
|
-
class MainWindowVieModel
|
58
|
-
{
|
59
|
-
public ObservableCollection<Person> People { get; }
|
60
|
-
|
61
|
-
public MainWindowVieModel()
|
62
|
-
{
|
63
|
-
People = new ObservableCollection<Person>
|
64
|
-
{
|
65
|
-
new Person { ID = 1, Name = "AAA", Sex = "男", Email = "xxx@example.com" },
|
66
|
-
};
|
67
|
-
}
|
68
|
-
}
|
69
|
-
|
70
|
-
public partial class MainWindow : Window
|
71
|
-
{
|
72
|
-
public MainWindow()
|
73
|
-
{
|
74
|
-
InitializeComponent();
|
75
|
-
}
|
76
|
-
}
|
77
|
-
}
|
1
|
+
> DataGridのMVVM形式での実装は下記を参考にしました
|
2
|
+
> https://www.fenet.jp/dotnet/column/tool/4287/
|
3
|
+
|
4
|
+
ほんとうですか!?似ても似つかないように思うのですが。。
|
5
|
+
|
6
|
+
コレクションと、個々のアイテムの区別がついていないように見受けられます。
|
7
|
+
|
8
|
+
コレクションとは、`ObservableCollection`や`List`・配列等複数のアイテムを持つものです(回答コードでいえば`People`)
|
9
|
+
|
10
|
+
個々のアイテムとは、コレクションの中のひとつひとつです(回答コードでいえば`Person`)
|
11
|
+
|
12
|
+
`DataGrid`の`ItemsSource`にはコレクションをバインドします。
|
13
|
+
|
14
|
+
`DataGridTextColumn`の段階では`DataContext`は個々のアイテムになっているので、`{Binding ParamA.ID}`ではなく`{Binding ID}`とプロパティだけでよいことになります。
|
15
|
+
|
16
|
+
```xml
|
17
|
+
<Window
|
18
|
+
x:Class="Questions353106.MainWindow"
|
19
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
+
xmlns:local="clr-namespace:Questions353106"
|
22
|
+
Width="800"
|
23
|
+
Height="450">
|
24
|
+
<Window.DataContext>
|
25
|
+
<local:MainWindowVieModel />
|
26
|
+
</Window.DataContext>
|
27
|
+
<Grid>
|
28
|
+
<DataGrid
|
29
|
+
AutoGenerateColumns="False"
|
30
|
+
IsReadOnly="True"
|
31
|
+
ItemsSource="{Binding People}">
|
32
|
+
<DataGrid.Columns>
|
33
|
+
<DataGridTextColumn Binding="{Binding ID}" Header="ID" />
|
34
|
+
<DataGridTextColumn Binding="{Binding Name}" Header="Name" />
|
35
|
+
<DataGridTextColumn Binding="{Binding Sex}" Header="Sex" />
|
36
|
+
<DataGridTextColumn Binding="{Binding Email}" Header="メール" />
|
37
|
+
</DataGrid.Columns>
|
38
|
+
</DataGrid>
|
39
|
+
</Grid>
|
40
|
+
</Window>
|
41
|
+
```
|
42
|
+
|
43
|
+
```cs
|
44
|
+
using System.Collections.ObjectModel;
|
45
|
+
using System.Windows;
|
46
|
+
|
47
|
+
namespace Questions353106
|
48
|
+
{
|
49
|
+
class Person
|
50
|
+
{
|
51
|
+
public int ID { get; set; }
|
52
|
+
public string Name { get; set; }
|
53
|
+
public string Sex { get; set; }
|
54
|
+
public string Email { get; set; }
|
55
|
+
}
|
56
|
+
|
57
|
+
class MainWindowVieModel
|
58
|
+
{
|
59
|
+
public ObservableCollection<Person> People { get; }
|
60
|
+
|
61
|
+
public MainWindowVieModel()
|
62
|
+
{
|
63
|
+
People = new ObservableCollection<Person>
|
64
|
+
{
|
65
|
+
new Person { ID = 1, Name = "AAA", Sex = "男", Email = "xxx@example.com" },
|
66
|
+
};
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
public partial class MainWindow : Window
|
71
|
+
{
|
72
|
+
public MainWindow()
|
73
|
+
{
|
74
|
+
InitializeComponent();
|
75
|
+
}
|
76
|
+
}
|
77
|
+
}
|
78
78
|
```
|