回答編集履歴

1

見直しキャンペーン中

2023/07/28 15:58

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,155 +1,78 @@
1
1
  > DataGridのMVVM形式での実装は下記を参考にしました
2
-
3
2
  > https://www.fenet.jp/dotnet/column/tool/4287/
4
-
5
-
6
3
 
7
4
  ほんとうですか!?似ても似つかないように思うのですが。。
8
5
 
9
-
10
-
11
6
  コレクションと、個々のアイテムの区別がついていないように見受けられます。
12
-
13
-
14
7
 
15
8
  コレクションとは、`ObservableCollection`や`List`・配列等複数のアイテムを持つものです(回答コードでいえば`People`)
16
9
 
17
-
18
-
19
10
  個々のアイテムとは、コレクションの中のひとつひとつです(回答コードでいえば`Person`)
20
-
21
-
22
11
 
23
12
  `DataGrid`の`ItemsSource`にはコレクションをバインドします。
24
13
 
25
-
26
-
27
14
  `DataGridTextColumn`の段階では`DataContext`は個々のアイテムになっているので、`{Binding ParamA.ID}`ではなく`{Binding ID}`とプロパティだけでよいことになります。
28
15
 
29
-
30
-
31
- ```xaml
16
+ ```xml
32
-
33
17
  <Window
34
-
35
18
  x:Class="Questions353106.MainWindow"
36
-
37
19
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
-
39
20
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
40
-
41
21
  xmlns:local="clr-namespace:Questions353106"
42
-
43
22
  Width="800"
44
-
45
23
  Height="450">
46
-
47
24
  <Window.DataContext>
48
-
49
25
  <local:MainWindowVieModel />
50
-
51
26
  </Window.DataContext>
52
-
53
27
  <Grid>
54
-
55
28
  <DataGrid
56
-
57
29
  AutoGenerateColumns="False"
58
-
59
30
  IsReadOnly="True"
60
-
61
31
  ItemsSource="{Binding People}">
62
-
63
32
  <DataGrid.Columns>
64
-
65
33
  <DataGridTextColumn Binding="{Binding ID}" Header="ID" />
66
-
67
34
  <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
68
-
69
35
  <DataGridTextColumn Binding="{Binding Sex}" Header="Sex" />
70
-
71
36
  <DataGridTextColumn Binding="{Binding Email}" Header="メール" />
72
-
73
37
  </DataGrid.Columns>
74
-
75
38
  </DataGrid>
76
-
77
39
  </Grid>
78
-
79
40
  </Window>
80
-
81
41
  ```
82
42
 
83
-
84
-
85
- ```C#
43
+ ```cs
86
-
87
44
  using System.Collections.ObjectModel;
88
-
89
45
  using System.Windows;
90
46
 
91
-
92
-
93
47
  namespace Questions353106
94
-
95
48
  {
96
-
97
49
  class Person
98
-
99
50
  {
100
-
101
51
  public int ID { get; set; }
102
-
103
52
  public string Name { get; set; }
104
-
105
53
  public string Sex { get; set; }
106
-
107
54
  public string Email { get; set; }
108
-
109
55
  }
110
56
 
111
-
112
-
113
57
  class MainWindowVieModel
114
-
115
58
  {
116
-
117
59
  public ObservableCollection<Person> People { get; }
118
60
 
119
-
120
-
121
61
  public MainWindowVieModel()
122
-
123
62
  {
124
-
125
63
  People = new ObservableCollection<Person>
126
-
127
64
  {
128
-
129
65
  new Person { ID = 1, Name = "AAA", Sex = "男", Email = "xxx@example.com" },
130
-
131
66
  };
132
-
133
67
  }
134
-
135
68
  }
136
69
 
137
-
138
-
139
70
  public partial class MainWindow : Window
140
-
141
71
  {
142
-
143
72
  public MainWindow()
144
-
145
73
  {
146
-
147
74
  InitializeComponent();
148
-
149
75
  }
150
-
151
76
  }
152
-
153
77
  }
154
-
155
78
  ```