回答編集履歴

3

見直しキャンペーン中

2023/08/14 09:27

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -4,7 +4,7 @@
4
4
  その中から1つを選んで`BordColor`に入れるんですから。よく考えれば当たり前ですよね?
5
5
 
6
6
  では`DataGrid`に`ComboBox`を入れる方法を調べてみましょう。
7
- 「wpf datagrid combobox」で検索してみます。
7
+ [wpf datagrid combobox](https://www.google.co.jp/search?q=wpf+datagrid+combobox)」で検索してみます。
8
8
 
9
9
  [DataGridComboBoxColumn](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagridcomboboxcolumn)というのがあるようです(実はこれ人気がないのですが、後で説明します)
10
10
 

2

見直しキャンペーン中

2023/07/27 15:08

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,515 +1,258 @@
1
1
  コンボボックスの選択肢は、`DataGrid`の行(`ObservableCollection<Person>`)とは全く別のコレクションのはずです。
2
2
 
3
-
4
-
5
3
  `string BordColor`に入れる選択肢であれば、`string[]`や`List<string>`等になるわけです。
6
-
7
4
  その中から1つを選んで`BordColor`に入れるんですから。よく考えれば当たり前ですよね?
8
5
 
9
-
10
-
11
6
  では`DataGrid`に`ComboBox`を入れる方法を調べてみましょう。
12
-
13
7
  「wpf datagrid combobox」で検索してみます。
14
8
 
15
-
16
-
17
9
  [DataGridComboBoxColumn](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagridcomboboxcolumn)というのがあるようです(実はこれ人気がないのですが、後で説明します)
18
10
 
19
-
20
-
21
11
  中段に`ItemsSource`の入れ方が書いてあります(翻訳がいまいちなので英語を見ます^^;
22
12
 
23
-
24
-
25
13
  * A static resource.
26
-
27
14
  `StaticResource`を使う方法。xamlで完結するので固定値な場合はこれでOK。
28
-
29
15
  * An x:Static code entity.
30
-
31
16
  コード上の`static`コレクションを引いてくる方法。実行時に作成したいとき。
32
-
33
17
  * An inline collection of ComboBoxItem types.
34
-
35
18
  どういう意味かちょっと分からなかったのですが、`DataGridTemplateColumn`のときのようです。
36
19
 
37
-
38
-
39
20
  [wpf - "An inline collection of ComboBoxItem" in a DataGridComboBoxColumn - Stack Overflow](https://stackoverflow.com/questions/36948539/an-inline-collection-of-comboboxitem-in-a-datagridcomboboxcolumn)
40
21
 
41
-
42
-
43
- ```xaml
22
+ ```xml
44
-
45
23
  <Window
46
-
47
24
  x:Class="Questions343018.MainWindow"
48
-
49
25
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
50
-
51
26
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
52
-
53
27
  xmlns:local="clr-namespace:Questions343018"
54
-
55
28
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
56
-
57
29
  Width="800"
58
-
59
30
  Height="450">
60
-
61
31
  <Window.Resources>
62
-
63
32
  <x:Array x:Key="Colors" Type="sys:String">
64
-
65
33
  <sys:String>NoColor</sys:String>
66
-
67
34
  <sys:String>Red</sys:String>
68
-
69
35
  <sys:String>Green</sys:String>
70
-
71
36
  <sys:String>Blue</sys:String>
72
-
73
37
  <sys:String>White</sys:String>
74
-
75
38
  </x:Array>
76
-
77
39
  </Window.Resources>
78
40
 
79
-
80
-
81
41
  <DockPanel>
82
-
83
42
  <StackPanel
84
-
85
43
  Margin="10"
86
-
87
44
  DockPanel.Dock="Top"
88
-
89
45
  Orientation="Horizontal">
90
-
91
46
  <Button
92
-
93
47
  MinWidth="100"
94
-
95
48
  Click="ReadButton_Click"
96
-
97
49
  Content="Read" />
98
-
99
50
  <Button
100
-
101
51
  MinWidth="100"
102
-
103
52
  Click="WriteButton_Click"
104
-
105
53
  Content="Write" />
106
-
107
54
  </StackPanel>
108
55
 
109
-
110
-
111
56
  <DataGrid
112
-
113
57
  AutoGenerateColumns="False"
114
-
115
58
  CanUserReorderColumns="False"
116
-
117
59
  CanUserSortColumns="False"
118
-
119
60
  ItemsSource="{Binding Persons}">
120
-
121
61
  <DataGrid.Columns>
122
-
123
62
  <DataGridTextColumn
124
-
125
63
  Binding="{Binding Num}"
126
-
127
64
  Header="番号"
128
-
129
65
  IsReadOnly="True" />
130
-
131
66
  <DataGridTextColumn Binding="{Binding Title}" Header="タイトル" />
132
67
 
133
-
134
-
135
68
  <!-- A static resource. -->
136
-
137
69
  <DataGridComboBoxColumn
138
-
139
70
  Header="線の色"
140
-
141
71
  ItemsSource="{StaticResource Colors}"
142
-
143
72
  SelectedValueBinding="{Binding BordColor}" />
144
73
 
145
-
146
-
147
74
  <!-- An x:Static code entity. -->
148
-
149
75
  <DataGridComboBoxColumn
150
-
151
76
  Header="線の色"
152
-
153
77
  ItemsSource="{x:Static local:MainWindow.Colors}"
154
-
155
78
  SelectedValueBinding="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
156
79
 
157
-
158
-
159
80
  <!-- An inline collection of ComboBoxItem types. -->
160
-
161
81
  <DataGridTemplateColumn Header="線の色">
162
-
163
82
  <DataGridTemplateColumn.CellTemplate>
164
-
165
83
  <DataTemplate>
166
-
167
84
  <ComboBox SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Content">
168
-
169
85
  <ComboBoxItem>NoColor</ComboBoxItem>
170
-
171
86
  <ComboBoxItem>Red</ComboBoxItem>
172
-
173
87
  <ComboBoxItem>Green</ComboBoxItem>
174
-
175
88
  <ComboBoxItem>Blue</ComboBoxItem>
176
-
177
89
  <ComboBoxItem>White</ComboBoxItem>
178
-
179
90
  </ComboBox>
180
-
181
91
  </DataTemplate>
182
-
183
92
  </DataGridTemplateColumn.CellTemplate>
184
-
185
93
  </DataGridTemplateColumn>
186
94
 
187
-
188
-
189
95
  <!--
190
-
191
96
  グダグダ書いてきたが、結論は一番上か↓でいいです^^;
192
-
193
97
  どちらがいいかは編集時の操作の好みで
194
-
195
98
  -->
196
-
197
99
  <!-- この場合UpdateSourceTrigger=PropertyChanged必須 -->
198
-
199
100
  <!--<DataGridTemplateColumn Header="線の色">
200
-
201
101
  <DataGridTemplateColumn.CellTemplate>
202
-
203
102
  <DataTemplate>
204
-
205
103
  <ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
206
-
207
104
  </DataTemplate>
208
-
209
105
  </DataGridTemplateColumn.CellTemplate>
210
-
211
106
  </DataGridTemplateColumn>-->
212
107
 
213
108
 
214
-
215
-
216
-
217
109
  </DataGrid.Columns>
218
-
219
110
  </DataGrid>
220
-
221
111
  </DockPanel>
222
-
223
112
  </Window>
224
-
225
113
  ```
226
114
 
227
-
228
-
229
- ```C#
115
+ ```cs
230
-
231
116
  using Microsoft.Win32;
232
-
233
117
  using System;
234
-
235
118
  using System.Collections.ObjectModel;
236
-
237
119
  using System.Diagnostics;
238
-
239
120
  using System.IO;
240
-
241
121
  using System.Text;
242
-
243
122
  using System.Windows;
244
123
 
245
-
246
-
247
124
  namespace Questions343018
248
-
249
125
  {
250
-
251
126
  public class Person
252
-
253
127
  {
254
-
255
128
  public int Num { get; set; }
256
-
257
129
  public string Title { get; set; }
258
-
259
130
  public string CurrentFolder { get; set; }
260
-
261
131
  public string PicName { get; set; }
262
-
263
132
  public string BordColor { get; set; }
264
133
 
265
-
266
-
267
134
  public Person(int num, string title, string currentFolder, string picName, string bordColor)
268
-
269
- {
135
+ {
270
-
271
136
  Num = num;
272
-
273
137
  Title = title;
274
-
275
138
  CurrentFolder = currentFolder;
276
-
277
139
  PicName = picName;
278
-
279
140
  BordColor = bordColor;
280
-
281
- }
141
+ }
282
-
283
-
284
142
 
285
143
  public override string ToString() => $"{Num},{Title},{CurrentFolder},{PicName},{BordColor}";
286
-
287
144
  }
288
145
 
289
-
290
-
291
146
  public partial class MainWindow : Window
292
-
293
147
  {
294
-
295
148
  public static string[] Colors = { "NoColor", "Red", "Green", "Blue", "White", };
296
149
 
297
-
298
-
299
150
  public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();
300
151
 
301
152
 
302
-
303
-
304
-
305
153
  public MainWindow()
306
-
307
- {
154
+ {
308
-
309
155
  InitializeComponent();
310
-
311
156
  DataContext = this;
312
157
 
313
-
314
-
315
158
  Dummy();
316
-
317
159
  ReadButton_Click(null, null);
318
-
319
- }
160
+ }
320
-
321
-
322
161
 
323
162
  private void ReadButton_Click(object sender, RoutedEventArgs e)
324
-
325
- {
163
+ {
326
-
327
164
  var appPath = AppDomain.CurrentDomain.BaseDirectory;
328
-
329
165
  var filePath = Path.Combine(appPath, "test.csv");
330
-
331
166
  Debug.WriteLine(filePath);
332
167
 
333
-
334
-
335
168
  if (File.Exists(filePath) == false)
336
-
337
- {
169
+ {
338
-
339
170
  return;
340
-
341
- }
171
+ }
342
-
343
-
344
172
 
345
173
  Persons.Clear();
346
-
347
174
  ReadFile(filePath);
348
-
349
- }
175
+ }
350
-
351
-
352
176
 
353
177
  public void WriteButton_Click(object sender, RoutedEventArgs e)
354
-
355
- {
178
+ {
356
-
357
179
  var dlg = new SaveFileDialog
358
-
359
- {
180
+ {
360
-
361
181
  InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal),
362
-
363
182
  Title = "保存先のファイルを選択してください",
364
-
365
183
  Filter = "CSVファイル(*.csv)|*.csv"
366
-
367
184
  };
368
185
 
369
-
370
-
371
186
  if (dlg.ShowDialog() == true)
372
-
373
- {
187
+ {
374
-
375
188
  WriteFile(dlg.FileName);
376
-
377
- }
189
+ }
378
-
379
- }
190
+ }
380
-
381
-
382
191
 
383
192
  private void ReadFile(string filePath)
384
-
385
- {
193
+ {
386
-
387
194
  try
388
-
389
- {
195
+ {
390
-
391
196
  using (var sr = new StreamReader(filePath, Encoding.UTF8))
392
-
393
197
  {
394
-
395
198
  string line = null;
396
-
397
199
  while ((line = sr.ReadLine()) != null)
398
-
399
200
  {
400
-
401
201
  var s = line.Split(',');
402
-
403
202
  Persons.Add(new Person(int.Parse(s[0]), s[1], s[2], s[3], s[4]));
404
-
405
203
  }
406
-
407
204
  }
408
-
409
- }
205
+ }
410
-
411
206
  catch (Exception e)
412
-
413
- {
207
+ {
414
-
415
208
  Debug.WriteLine(e.Message);
416
-
417
- }
209
+ }
418
-
419
- }
210
+ }
420
-
421
-
422
211
 
423
212
  private void WriteFile(string filePath)
424
-
425
- {
213
+ {
426
-
427
214
  try
428
-
429
- {
215
+ {
430
-
431
216
  using (var sw = new StreamWriter(filePath, false, Encoding.UTF8))
432
-
433
217
  {
434
-
435
218
  foreach (var person in Persons)
436
-
437
219
  {
438
-
439
220
  var line = person.ToString();
440
-
441
221
  sw.WriteLine(line);
442
-
443
222
  }
444
-
445
223
  }
446
-
447
224
  MessageBox.Show("保存しました");
448
-
449
- }
225
+ }
450
-
451
226
  catch (Exception e)
452
-
453
- {
227
+ {
454
-
455
228
  Debug.WriteLine(e.Message);
456
-
457
- }
229
+ }
458
-
459
- }
230
+ }
460
-
461
-
462
231
 
463
232
  private void Dummy()
464
-
465
- {
233
+ {
466
-
467
234
  var appPath = AppDomain.CurrentDomain.BaseDirectory;
468
-
469
235
  var filePath = Path.Combine(appPath, "test.csv");
470
-
471
236
  var text = @"
472
-
473
237
  1,Title1,CurrentFolder1,PicName1,Red
474
-
475
238
  2,Title2,CurrentFolder2,PicName2,White
476
-
477
239
  3,Title3,CurrentFolder3,PicName3,NoColor
478
-
479
240
  ".Trim();
480
-
481
241
  File.WriteAllText(filePath, text);
482
-
483
- }
242
+ }
484
-
485
243
  }
486
-
487
244
  }
488
-
489
245
  ```
490
246
 
491
-
492
-
493
247
  `DataGridComboBoxColumn`を使ってみるとわかるのですが、変更までのクリック回数が多くあまり使いやすくないです。
494
-
495
248
  そのため`DataGridTemplateColumn`に`ComboBox`を入れる人が多いです(コメントになっている4つ目の方法)
496
249
 
497
-
498
-
499
250
  通常は`UpdateSourceTrigger=PropertyChanged`はいりません。
500
-
501
251
  同じ値を3つ出しているので、変更されていないように見えると変かなと思ったためです(1つ目はついていないですが、変更が確定するのは行のフォーカスが動いたとき)
502
252
 
503
-
504
-
505
253
  ---
506
254
 
507
-
508
-
509
255
  > WPFで検索してもWinFormを利用しているものが多く、
510
256
 
511
-
512
-
513
- キーワードに「wpf」を入れても`WinForms`の情報が出てきます?
257
+ キーワードに「wpf」を入れてもWinFormsの情報が出てきます?
514
-
515
- 例えば「c# combobox」で`WinForms`ばかりになるのはまあ普通ですが、「wpf combobox」であれば`WinForms`の話は全くヒットしませんが。
258
+ 例えば「[c# combobox](https://www.google.co.jp/search?q=c%23+combobox)」でWinFormsばかりになるのはまあ普通ですが、「[wpf combobox](https://www.google.co.jp/search?q=wpf+combobox)」であればWinFormsの話は全くヒットしませんが。

1

UpdateSourceTrigger=PropertyChanged

2021/06/10 04:44

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -194,13 +194,15 @@
194
194
 
195
195
  -->
196
196
 
197
+ <!-- この場合UpdateSourceTrigger=PropertyChanged必須 -->
198
+
197
199
  <!--<DataGridTemplateColumn Header="線の色">
198
200
 
199
201
  <DataGridTemplateColumn.CellTemplate>
200
202
 
201
203
  <DataTemplate>
202
204
 
203
- <ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor}" />
205
+ <ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
204
206
 
205
207
  </DataTemplate>
206
208
 
@@ -210,6 +212,8 @@
210
212
 
211
213
 
212
214
 
215
+
216
+
213
217
  </DataGrid.Columns>
214
218
 
215
219
  </DataGrid>