回答編集履歴
3
見直しキャンペーン中
answer
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
見直しキャンペーン中
answer
CHANGED
@@ -1,258 +1,258 @@
|
|
1
|
-
コンボボックスの選択肢は、`DataGrid`の行(`ObservableCollection<Person>`)とは全く別のコレクションのはずです。
|
2
|
-
|
3
|
-
`string BordColor`に入れる選択肢であれば、`string[]`や`List<string>`等になるわけです。
|
4
|
-
その中から1つを選んで`BordColor`に入れるんですから。よく考えれば当たり前ですよね?
|
5
|
-
|
6
|
-
では`DataGrid`に`ComboBox`を入れる方法を調べてみましょう。
|
7
|
-
「wpf datagrid combobox」で検索してみます。
|
8
|
-
|
9
|
-
[DataGridComboBoxColumn](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagridcomboboxcolumn)というのがあるようです(実はこれ人気がないのですが、後で説明します)
|
10
|
-
|
11
|
-
中段に`ItemsSource`の入れ方が書いてあります(翻訳がいまいちなので英語を見ます^^;
|
12
|
-
|
13
|
-
* A static resource.
|
14
|
-
`StaticResource`を使う方法。xamlで完結するので固定値な場合はこれでOK。
|
15
|
-
* An x:Static code entity.
|
16
|
-
コード上の`static`コレクションを引いてくる方法。実行時に作成したいとき。
|
17
|
-
* An inline collection of ComboBoxItem types.
|
18
|
-
どういう意味かちょっと分からなかったのですが、`DataGridTemplateColumn`のときのようです。
|
19
|
-
|
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)
|
21
|
-
|
22
|
-
```
|
23
|
-
<Window
|
24
|
-
x:Class="Questions343018.MainWindow"
|
25
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
26
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
27
|
-
xmlns:local="clr-namespace:Questions343018"
|
28
|
-
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
29
|
-
Width="800"
|
30
|
-
Height="450">
|
31
|
-
<Window.Resources>
|
32
|
-
<x:Array x:Key="Colors" Type="sys:String">
|
33
|
-
<sys:String>NoColor</sys:String>
|
34
|
-
<sys:String>Red</sys:String>
|
35
|
-
<sys:String>Green</sys:String>
|
36
|
-
<sys:String>Blue</sys:String>
|
37
|
-
<sys:String>White</sys:String>
|
38
|
-
</x:Array>
|
39
|
-
</Window.Resources>
|
40
|
-
|
41
|
-
<DockPanel>
|
42
|
-
<StackPanel
|
43
|
-
Margin="10"
|
44
|
-
DockPanel.Dock="Top"
|
45
|
-
Orientation="Horizontal">
|
46
|
-
<Button
|
47
|
-
MinWidth="100"
|
48
|
-
Click="ReadButton_Click"
|
49
|
-
Content="Read" />
|
50
|
-
<Button
|
51
|
-
MinWidth="100"
|
52
|
-
Click="WriteButton_Click"
|
53
|
-
Content="Write" />
|
54
|
-
</StackPanel>
|
55
|
-
|
56
|
-
<DataGrid
|
57
|
-
AutoGenerateColumns="False"
|
58
|
-
CanUserReorderColumns="False"
|
59
|
-
CanUserSortColumns="False"
|
60
|
-
ItemsSource="{Binding Persons}">
|
61
|
-
<DataGrid.Columns>
|
62
|
-
<DataGridTextColumn
|
63
|
-
Binding="{Binding Num}"
|
64
|
-
Header="番号"
|
65
|
-
IsReadOnly="True" />
|
66
|
-
<DataGridTextColumn Binding="{Binding Title}" Header="タイトル" />
|
67
|
-
|
68
|
-
<!-- A static resource. -->
|
69
|
-
<DataGridComboBoxColumn
|
70
|
-
Header="線の色"
|
71
|
-
ItemsSource="{StaticResource Colors}"
|
72
|
-
SelectedValueBinding="{Binding BordColor}" />
|
73
|
-
|
74
|
-
<!-- An x:Static code entity. -->
|
75
|
-
<DataGridComboBoxColumn
|
76
|
-
Header="線の色"
|
77
|
-
ItemsSource="{x:Static local:MainWindow.Colors}"
|
78
|
-
SelectedValueBinding="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
|
79
|
-
|
80
|
-
<!-- An inline collection of ComboBoxItem types. -->
|
81
|
-
<DataGridTemplateColumn Header="線の色">
|
82
|
-
<DataGridTemplateColumn.CellTemplate>
|
83
|
-
<DataTemplate>
|
84
|
-
<ComboBox SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Content">
|
85
|
-
<ComboBoxItem>NoColor</ComboBoxItem>
|
86
|
-
<ComboBoxItem>Red</ComboBoxItem>
|
87
|
-
<ComboBoxItem>Green</ComboBoxItem>
|
88
|
-
<ComboBoxItem>Blue</ComboBoxItem>
|
89
|
-
<ComboBoxItem>White</ComboBoxItem>
|
90
|
-
</ComboBox>
|
91
|
-
</DataTemplate>
|
92
|
-
</DataGridTemplateColumn.CellTemplate>
|
93
|
-
</DataGridTemplateColumn>
|
94
|
-
|
95
|
-
<!--
|
96
|
-
グダグダ書いてきたが、結論は一番上か↓でいいです^^;
|
97
|
-
どちらがいいかは編集時の操作の好みで
|
98
|
-
-->
|
99
|
-
<!-- この場合UpdateSourceTrigger=PropertyChanged必須 -->
|
100
|
-
<!--<DataGridTemplateColumn Header="線の色">
|
101
|
-
<DataGridTemplateColumn.CellTemplate>
|
102
|
-
<DataTemplate>
|
103
|
-
<ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
|
104
|
-
</DataTemplate>
|
105
|
-
</DataGridTemplateColumn.CellTemplate>
|
106
|
-
</DataGridTemplateColumn>-->
|
107
|
-
|
108
|
-
|
109
|
-
</DataGrid.Columns>
|
110
|
-
</DataGrid>
|
111
|
-
</DockPanel>
|
112
|
-
</Window>
|
113
|
-
```
|
114
|
-
|
115
|
-
```
|
116
|
-
using Microsoft.Win32;
|
117
|
-
using System;
|
118
|
-
using System.Collections.ObjectModel;
|
119
|
-
using System.Diagnostics;
|
120
|
-
using System.IO;
|
121
|
-
using System.Text;
|
122
|
-
using System.Windows;
|
123
|
-
|
124
|
-
namespace Questions343018
|
125
|
-
{
|
126
|
-
public class Person
|
127
|
-
{
|
128
|
-
public int Num { get; set; }
|
129
|
-
public string Title { get; set; }
|
130
|
-
public string CurrentFolder { get; set; }
|
131
|
-
public string PicName { get; set; }
|
132
|
-
public string BordColor { get; set; }
|
133
|
-
|
134
|
-
public Person(int num, string title, string currentFolder, string picName, string bordColor)
|
135
|
-
{
|
136
|
-
Num = num;
|
137
|
-
Title = title;
|
138
|
-
CurrentFolder = currentFolder;
|
139
|
-
PicName = picName;
|
140
|
-
BordColor = bordColor;
|
141
|
-
}
|
142
|
-
|
143
|
-
public override string ToString() => $"{Num},{Title},{CurrentFolder},{PicName},{BordColor}";
|
144
|
-
}
|
145
|
-
|
146
|
-
public partial class MainWindow : Window
|
147
|
-
{
|
148
|
-
public static string[] Colors = { "NoColor", "Red", "Green", "Blue", "White", };
|
149
|
-
|
150
|
-
public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();
|
151
|
-
|
152
|
-
|
153
|
-
public MainWindow()
|
154
|
-
{
|
155
|
-
InitializeComponent();
|
156
|
-
DataContext = this;
|
157
|
-
|
158
|
-
Dummy();
|
159
|
-
ReadButton_Click(null, null);
|
160
|
-
}
|
161
|
-
|
162
|
-
private void ReadButton_Click(object sender, RoutedEventArgs e)
|
163
|
-
{
|
164
|
-
var appPath = AppDomain.CurrentDomain.BaseDirectory;
|
165
|
-
var filePath = Path.Combine(appPath, "test.csv");
|
166
|
-
Debug.WriteLine(filePath);
|
167
|
-
|
168
|
-
if (File.Exists(filePath) == false)
|
169
|
-
{
|
170
|
-
return;
|
171
|
-
}
|
172
|
-
|
173
|
-
Persons.Clear();
|
174
|
-
ReadFile(filePath);
|
175
|
-
}
|
176
|
-
|
177
|
-
public void WriteButton_Click(object sender, RoutedEventArgs e)
|
178
|
-
{
|
179
|
-
var dlg = new SaveFileDialog
|
180
|
-
{
|
181
|
-
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal),
|
182
|
-
Title = "保存先のファイルを選択してください",
|
183
|
-
Filter = "CSVファイル(*.csv)|*.csv"
|
184
|
-
};
|
185
|
-
|
186
|
-
if (dlg.ShowDialog() == true)
|
187
|
-
{
|
188
|
-
WriteFile(dlg.FileName);
|
189
|
-
}
|
190
|
-
}
|
191
|
-
|
192
|
-
private void ReadFile(string filePath)
|
193
|
-
{
|
194
|
-
try
|
195
|
-
{
|
196
|
-
using (var sr = new StreamReader(filePath, Encoding.UTF8))
|
197
|
-
{
|
198
|
-
string line = null;
|
199
|
-
while ((line = sr.ReadLine()) != null)
|
200
|
-
{
|
201
|
-
var s = line.Split(',');
|
202
|
-
Persons.Add(new Person(int.Parse(s[0]), s[1], s[2], s[3], s[4]));
|
203
|
-
}
|
204
|
-
}
|
205
|
-
}
|
206
|
-
catch (Exception e)
|
207
|
-
{
|
208
|
-
Debug.WriteLine(e.Message);
|
209
|
-
}
|
210
|
-
}
|
211
|
-
|
212
|
-
private void WriteFile(string filePath)
|
213
|
-
{
|
214
|
-
try
|
215
|
-
{
|
216
|
-
using (var sw = new StreamWriter(filePath, false, Encoding.UTF8))
|
217
|
-
{
|
218
|
-
foreach (var person in Persons)
|
219
|
-
{
|
220
|
-
var line = person.ToString();
|
221
|
-
sw.WriteLine(line);
|
222
|
-
}
|
223
|
-
}
|
224
|
-
MessageBox.Show("保存しました");
|
225
|
-
}
|
226
|
-
catch (Exception e)
|
227
|
-
{
|
228
|
-
Debug.WriteLine(e.Message);
|
229
|
-
}
|
230
|
-
}
|
231
|
-
|
232
|
-
private void Dummy()
|
233
|
-
{
|
234
|
-
var appPath = AppDomain.CurrentDomain.BaseDirectory;
|
235
|
-
var filePath = Path.Combine(appPath, "test.csv");
|
236
|
-
var text = @"
|
237
|
-
1,Title1,CurrentFolder1,PicName1,Red
|
238
|
-
2,Title2,CurrentFolder2,PicName2,White
|
239
|
-
3,Title3,CurrentFolder3,PicName3,NoColor
|
240
|
-
".Trim();
|
241
|
-
File.WriteAllText(filePath, text);
|
242
|
-
}
|
243
|
-
}
|
244
|
-
}
|
245
|
-
```
|
246
|
-
|
247
|
-
`DataGridComboBoxColumn`を使ってみるとわかるのですが、変更までのクリック回数が多くあまり使いやすくないです。
|
248
|
-
そのため`DataGridTemplateColumn`に`ComboBox`を入れる人が多いです(コメントになっている4つ目の方法)
|
249
|
-
|
250
|
-
通常は`UpdateSourceTrigger=PropertyChanged`はいりません。
|
251
|
-
同じ値を3つ出しているので、変更されていないように見えると変かなと思ったためです(1つ目はついていないですが、変更が確定するのは行のフォーカスが動いたとき)
|
252
|
-
|
253
|
-
---
|
254
|
-
|
255
|
-
> WPFで検索してもWinFormを利用しているものが多く、
|
256
|
-
|
257
|
-
キーワードに「wpf」を入れても
|
258
|
-
例えば「c# combobox」で
|
1
|
+
コンボボックスの選択肢は、`DataGrid`の行(`ObservableCollection<Person>`)とは全く別のコレクションのはずです。
|
2
|
+
|
3
|
+
`string BordColor`に入れる選択肢であれば、`string[]`や`List<string>`等になるわけです。
|
4
|
+
その中から1つを選んで`BordColor`に入れるんですから。よく考えれば当たり前ですよね?
|
5
|
+
|
6
|
+
では`DataGrid`に`ComboBox`を入れる方法を調べてみましょう。
|
7
|
+
「wpf datagrid combobox」で検索してみます。
|
8
|
+
|
9
|
+
[DataGridComboBoxColumn](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagridcomboboxcolumn)というのがあるようです(実はこれ人気がないのですが、後で説明します)
|
10
|
+
|
11
|
+
中段に`ItemsSource`の入れ方が書いてあります(翻訳がいまいちなので英語を見ます^^;
|
12
|
+
|
13
|
+
* A static resource.
|
14
|
+
`StaticResource`を使う方法。xamlで完結するので固定値な場合はこれでOK。
|
15
|
+
* An x:Static code entity.
|
16
|
+
コード上の`static`コレクションを引いてくる方法。実行時に作成したいとき。
|
17
|
+
* An inline collection of ComboBoxItem types.
|
18
|
+
どういう意味かちょっと分からなかったのですが、`DataGridTemplateColumn`のときのようです。
|
19
|
+
|
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)
|
21
|
+
|
22
|
+
```xml
|
23
|
+
<Window
|
24
|
+
x:Class="Questions343018.MainWindow"
|
25
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
26
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
27
|
+
xmlns:local="clr-namespace:Questions343018"
|
28
|
+
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
29
|
+
Width="800"
|
30
|
+
Height="450">
|
31
|
+
<Window.Resources>
|
32
|
+
<x:Array x:Key="Colors" Type="sys:String">
|
33
|
+
<sys:String>NoColor</sys:String>
|
34
|
+
<sys:String>Red</sys:String>
|
35
|
+
<sys:String>Green</sys:String>
|
36
|
+
<sys:String>Blue</sys:String>
|
37
|
+
<sys:String>White</sys:String>
|
38
|
+
</x:Array>
|
39
|
+
</Window.Resources>
|
40
|
+
|
41
|
+
<DockPanel>
|
42
|
+
<StackPanel
|
43
|
+
Margin="10"
|
44
|
+
DockPanel.Dock="Top"
|
45
|
+
Orientation="Horizontal">
|
46
|
+
<Button
|
47
|
+
MinWidth="100"
|
48
|
+
Click="ReadButton_Click"
|
49
|
+
Content="Read" />
|
50
|
+
<Button
|
51
|
+
MinWidth="100"
|
52
|
+
Click="WriteButton_Click"
|
53
|
+
Content="Write" />
|
54
|
+
</StackPanel>
|
55
|
+
|
56
|
+
<DataGrid
|
57
|
+
AutoGenerateColumns="False"
|
58
|
+
CanUserReorderColumns="False"
|
59
|
+
CanUserSortColumns="False"
|
60
|
+
ItemsSource="{Binding Persons}">
|
61
|
+
<DataGrid.Columns>
|
62
|
+
<DataGridTextColumn
|
63
|
+
Binding="{Binding Num}"
|
64
|
+
Header="番号"
|
65
|
+
IsReadOnly="True" />
|
66
|
+
<DataGridTextColumn Binding="{Binding Title}" Header="タイトル" />
|
67
|
+
|
68
|
+
<!-- A static resource. -->
|
69
|
+
<DataGridComboBoxColumn
|
70
|
+
Header="線の色"
|
71
|
+
ItemsSource="{StaticResource Colors}"
|
72
|
+
SelectedValueBinding="{Binding BordColor}" />
|
73
|
+
|
74
|
+
<!-- An x:Static code entity. -->
|
75
|
+
<DataGridComboBoxColumn
|
76
|
+
Header="線の色"
|
77
|
+
ItemsSource="{x:Static local:MainWindow.Colors}"
|
78
|
+
SelectedValueBinding="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
|
79
|
+
|
80
|
+
<!-- An inline collection of ComboBoxItem types. -->
|
81
|
+
<DataGridTemplateColumn Header="線の色">
|
82
|
+
<DataGridTemplateColumn.CellTemplate>
|
83
|
+
<DataTemplate>
|
84
|
+
<ComboBox SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Content">
|
85
|
+
<ComboBoxItem>NoColor</ComboBoxItem>
|
86
|
+
<ComboBoxItem>Red</ComboBoxItem>
|
87
|
+
<ComboBoxItem>Green</ComboBoxItem>
|
88
|
+
<ComboBoxItem>Blue</ComboBoxItem>
|
89
|
+
<ComboBoxItem>White</ComboBoxItem>
|
90
|
+
</ComboBox>
|
91
|
+
</DataTemplate>
|
92
|
+
</DataGridTemplateColumn.CellTemplate>
|
93
|
+
</DataGridTemplateColumn>
|
94
|
+
|
95
|
+
<!--
|
96
|
+
グダグダ書いてきたが、結論は一番上か↓でいいです^^;
|
97
|
+
どちらがいいかは編集時の操作の好みで
|
98
|
+
-->
|
99
|
+
<!-- この場合UpdateSourceTrigger=PropertyChanged必須 -->
|
100
|
+
<!--<DataGridTemplateColumn Header="線の色">
|
101
|
+
<DataGridTemplateColumn.CellTemplate>
|
102
|
+
<DataTemplate>
|
103
|
+
<ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
|
104
|
+
</DataTemplate>
|
105
|
+
</DataGridTemplateColumn.CellTemplate>
|
106
|
+
</DataGridTemplateColumn>-->
|
107
|
+
|
108
|
+
|
109
|
+
</DataGrid.Columns>
|
110
|
+
</DataGrid>
|
111
|
+
</DockPanel>
|
112
|
+
</Window>
|
113
|
+
```
|
114
|
+
|
115
|
+
```cs
|
116
|
+
using Microsoft.Win32;
|
117
|
+
using System;
|
118
|
+
using System.Collections.ObjectModel;
|
119
|
+
using System.Diagnostics;
|
120
|
+
using System.IO;
|
121
|
+
using System.Text;
|
122
|
+
using System.Windows;
|
123
|
+
|
124
|
+
namespace Questions343018
|
125
|
+
{
|
126
|
+
public class Person
|
127
|
+
{
|
128
|
+
public int Num { get; set; }
|
129
|
+
public string Title { get; set; }
|
130
|
+
public string CurrentFolder { get; set; }
|
131
|
+
public string PicName { get; set; }
|
132
|
+
public string BordColor { get; set; }
|
133
|
+
|
134
|
+
public Person(int num, string title, string currentFolder, string picName, string bordColor)
|
135
|
+
{
|
136
|
+
Num = num;
|
137
|
+
Title = title;
|
138
|
+
CurrentFolder = currentFolder;
|
139
|
+
PicName = picName;
|
140
|
+
BordColor = bordColor;
|
141
|
+
}
|
142
|
+
|
143
|
+
public override string ToString() => $"{Num},{Title},{CurrentFolder},{PicName},{BordColor}";
|
144
|
+
}
|
145
|
+
|
146
|
+
public partial class MainWindow : Window
|
147
|
+
{
|
148
|
+
public static string[] Colors = { "NoColor", "Red", "Green", "Blue", "White", };
|
149
|
+
|
150
|
+
public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();
|
151
|
+
|
152
|
+
|
153
|
+
public MainWindow()
|
154
|
+
{
|
155
|
+
InitializeComponent();
|
156
|
+
DataContext = this;
|
157
|
+
|
158
|
+
Dummy();
|
159
|
+
ReadButton_Click(null, null);
|
160
|
+
}
|
161
|
+
|
162
|
+
private void ReadButton_Click(object sender, RoutedEventArgs e)
|
163
|
+
{
|
164
|
+
var appPath = AppDomain.CurrentDomain.BaseDirectory;
|
165
|
+
var filePath = Path.Combine(appPath, "test.csv");
|
166
|
+
Debug.WriteLine(filePath);
|
167
|
+
|
168
|
+
if (File.Exists(filePath) == false)
|
169
|
+
{
|
170
|
+
return;
|
171
|
+
}
|
172
|
+
|
173
|
+
Persons.Clear();
|
174
|
+
ReadFile(filePath);
|
175
|
+
}
|
176
|
+
|
177
|
+
public void WriteButton_Click(object sender, RoutedEventArgs e)
|
178
|
+
{
|
179
|
+
var dlg = new SaveFileDialog
|
180
|
+
{
|
181
|
+
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal),
|
182
|
+
Title = "保存先のファイルを選択してください",
|
183
|
+
Filter = "CSVファイル(*.csv)|*.csv"
|
184
|
+
};
|
185
|
+
|
186
|
+
if (dlg.ShowDialog() == true)
|
187
|
+
{
|
188
|
+
WriteFile(dlg.FileName);
|
189
|
+
}
|
190
|
+
}
|
191
|
+
|
192
|
+
private void ReadFile(string filePath)
|
193
|
+
{
|
194
|
+
try
|
195
|
+
{
|
196
|
+
using (var sr = new StreamReader(filePath, Encoding.UTF8))
|
197
|
+
{
|
198
|
+
string line = null;
|
199
|
+
while ((line = sr.ReadLine()) != null)
|
200
|
+
{
|
201
|
+
var s = line.Split(',');
|
202
|
+
Persons.Add(new Person(int.Parse(s[0]), s[1], s[2], s[3], s[4]));
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
catch (Exception e)
|
207
|
+
{
|
208
|
+
Debug.WriteLine(e.Message);
|
209
|
+
}
|
210
|
+
}
|
211
|
+
|
212
|
+
private void WriteFile(string filePath)
|
213
|
+
{
|
214
|
+
try
|
215
|
+
{
|
216
|
+
using (var sw = new StreamWriter(filePath, false, Encoding.UTF8))
|
217
|
+
{
|
218
|
+
foreach (var person in Persons)
|
219
|
+
{
|
220
|
+
var line = person.ToString();
|
221
|
+
sw.WriteLine(line);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
MessageBox.Show("保存しました");
|
225
|
+
}
|
226
|
+
catch (Exception e)
|
227
|
+
{
|
228
|
+
Debug.WriteLine(e.Message);
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
private void Dummy()
|
233
|
+
{
|
234
|
+
var appPath = AppDomain.CurrentDomain.BaseDirectory;
|
235
|
+
var filePath = Path.Combine(appPath, "test.csv");
|
236
|
+
var text = @"
|
237
|
+
1,Title1,CurrentFolder1,PicName1,Red
|
238
|
+
2,Title2,CurrentFolder2,PicName2,White
|
239
|
+
3,Title3,CurrentFolder3,PicName3,NoColor
|
240
|
+
".Trim();
|
241
|
+
File.WriteAllText(filePath, text);
|
242
|
+
}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
```
|
246
|
+
|
247
|
+
`DataGridComboBoxColumn`を使ってみるとわかるのですが、変更までのクリック回数が多くあまり使いやすくないです。
|
248
|
+
そのため`DataGridTemplateColumn`に`ComboBox`を入れる人が多いです(コメントになっている4つ目の方法)
|
249
|
+
|
250
|
+
通常は`UpdateSourceTrigger=PropertyChanged`はいりません。
|
251
|
+
同じ値を3つ出しているので、変更されていないように見えると変かなと思ったためです(1つ目はついていないですが、変更が確定するのは行のフォーカスが動いたとき)
|
252
|
+
|
253
|
+
---
|
254
|
+
|
255
|
+
> WPFで検索してもWinFormを利用しているものが多く、
|
256
|
+
|
257
|
+
キーワードに「wpf」を入れても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
answer
CHANGED
@@ -96,14 +96,16 @@
|
|
96
96
|
グダグダ書いてきたが、結論は一番上か↓でいいです^^;
|
97
97
|
どちらがいいかは編集時の操作の好みで
|
98
98
|
-->
|
99
|
+
<!-- この場合UpdateSourceTrigger=PropertyChanged必須 -->
|
99
100
|
<!--<DataGridTemplateColumn Header="線の色">
|
100
101
|
<DataGridTemplateColumn.CellTemplate>
|
101
102
|
<DataTemplate>
|
102
|
-
<ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor}" />
|
103
|
+
<ComboBox ItemsSource="{StaticResource Colors}" SelectedValue="{Binding BordColor, UpdateSourceTrigger=PropertyChanged}" />
|
103
104
|
</DataTemplate>
|
104
105
|
</DataGridTemplateColumn.CellTemplate>
|
105
106
|
</DataGridTemplateColumn>-->
|
106
107
|
|
108
|
+
|
107
109
|
</DataGrid.Columns>
|
108
110
|
</DataGrid>
|
109
111
|
</DockPanel>
|