回答編集履歴

1

見直しキャンペーン中

2023/07/23 09:02

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,265 +1,133 @@
1
1
  `ComboBox`の選択肢は個々のアイテム(`DataGrid`の行)の1項目であって、全く別のものであるはずです。
2
2
 
3
-
4
-
5
3
  なので`Gender`と`Item`に分けました。
6
-
7
4
  `Gender`はクラスでもいいのですが、扱いにくいので列挙型にしました。
8
-
9
5
  列挙型と表示文字列の対応付けはいろいろありますが、元コードと近い`Dictionary`方式にしました。
10
6
 
7
+ ```xml
8
+ <Window
9
+ x:Class="Questions302281.MainWindow"
10
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
11
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
12
+ Width="530"
13
+ Height="350">
14
+ <Window.Resources>
15
+ <CollectionViewSource x:Key="GenderDictSource" Source="{Binding GenderDict}" />
16
+ </Window.Resources>
17
+ <Grid>
18
+ <DataGrid
19
+ AutoGenerateColumns="False"
20
+ CanUserAddRows="False"
21
+ ItemsSource="{Binding Items}">
22
+ <DataGrid.Columns>
23
+ <DataGridTemplateColumn Header="金額(税率)">
24
+ <DataGridTemplateColumn.CellTemplate>
25
+ <DataTemplate>
26
+ <StackPanel Orientation="Horizontal">
27
+ <TextBlock Text="{Binding Price, StringFormat={}{0} 円}" />
28
+ <TextBlock Text="{Binding Tax, StringFormat=({0}%)}" />
29
+ </StackPanel>
30
+ </DataTemplate>
31
+ </DataGridTemplateColumn.CellTemplate>
32
+ </DataGridTemplateColumn>
33
+ <!--<DataGridTextColumn Header="金額(税率)">
34
+ <DataGridTextColumn.Binding>
35
+ <MultiBinding StringFormat="{}{0} 円({1}%)">
36
+ <Binding Path="Price" />
37
+ <Binding Path="Tax" />
38
+ </MultiBinding>
39
+ </DataGridTextColumn.Binding>
40
+ </DataGridTextColumn>-->
11
41
 
12
-
13
- ```xaml
14
-
15
- <Window
16
-
17
- x:Class="Questions302281.MainWindow"
18
-
19
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
-
21
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
-
23
- Width="530"
24
-
25
- Height="350">
26
-
27
- <Window.Resources>
28
-
29
- <CollectionViewSource x:Key="GenderDictSource" Source="{Binding GenderDict}" />
30
-
31
- </Window.Resources>
32
-
33
- <Grid>
34
-
35
- <DataGrid
36
-
37
- AutoGenerateColumns="False"
38
-
39
- CanUserAddRows="False"
40
-
41
- ItemsSource="{Binding Items}">
42
-
43
- <DataGrid.Columns>
44
-
45
- <DataGridTemplateColumn Header="金額(税率)">
42
+ <DataGridTemplateColumn Header="コンボボックス列">
46
-
47
43
  <DataGridTemplateColumn.CellTemplate>
48
-
49
44
  <DataTemplate>
50
-
45
+ <ComboBox
46
+ DisplayMemberPath="Value"
47
+ ItemsSource="{Binding DataContext.GenderDict, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
48
+ SelectedValue="{Binding Sex, UpdateSourceTrigger=PropertyChanged}"
51
- <StackPanel Orientation="Horizontal">
49
+ SelectedValuePath="Key" />
52
-
53
- <TextBlock Text="{Binding Price, StringFormat={}{0} 円}" />
54
-
55
- <TextBlock Text="{Binding Tax, StringFormat=({0}%)}" />
56
-
57
- </StackPanel>
58
-
59
50
  </DataTemplate>
60
-
61
51
  </DataGridTemplateColumn.CellTemplate>
62
-
63
52
  </DataGridTemplateColumn>
64
53
 
65
- <!--<DataGridTextColumn Header="金額(税率)">
66
-
67
- <DataGridTextColumn.Binding>
68
-
69
- <MultiBinding StringFormat="{}{0} 円({1}%)">
70
-
71
- <Binding Path="Price" />
72
-
73
- <Binding Path="Tax" />
74
-
75
- </MultiBinding>
76
-
77
- </DataGridTextColumn.Binding>
78
-
79
- </DataGridTextColumn>-->
80
-
81
-
82
-
83
- <DataGridTemplateColumn Header="コンボボックス列">
84
-
85
- <DataGridTemplateColumn.CellTemplate>
86
-
87
- <DataTemplate>
88
-
89
- <ComboBox
90
-
91
- DisplayMemberPath="Value"
92
-
93
- ItemsSource="{Binding DataContext.GenderDict, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
94
-
95
- SelectedValue="{Binding Sex, UpdateSourceTrigger=PropertyChanged}"
96
-
97
- SelectedValuePath="Key" />
98
-
99
- </DataTemplate>
100
-
101
- </DataGridTemplateColumn.CellTemplate>
102
-
103
- </DataGridTemplateColumn>
104
-
105
-
106
-
107
54
  <DataGridComboBoxColumn
108
-
109
55
  DisplayMemberPath="Value"
110
-
111
56
  Header="DataGridComboBoxColumn"
112
-
113
57
  ItemsSource="{Binding Source={StaticResource GenderDictSource}}"
114
-
115
58
  SelectedValueBinding="{Binding Sex}"
116
-
117
59
  SelectedValuePath="Key" />
118
-
119
60
  </DataGrid.Columns>
120
-
121
61
  </DataGrid>
122
62
 
123
-
124
-
125
63
  <Button
126
-
127
64
  HorizontalAlignment="Left"
128
-
129
65
  VerticalAlignment="Bottom"
130
-
131
66
  Click="Button_Click"
132
-
133
67
  Content="値の確認" />
134
-
135
68
  </Grid>
136
-
137
69
  </Window>
138
-
139
70
  ```
140
71
 
141
-
142
-
143
- ```C#
72
+ ```cs
144
-
145
73
  using System.Collections.Generic;
146
-
147
74
  using System.Collections.ObjectModel;
148
-
149
75
  using System.Diagnostics;
150
-
151
76
  using System.Windows;
152
77
 
153
-
154
-
155
78
  namespace Questions302281
156
-
157
79
  {
158
-
159
80
  public enum Gender
160
-
161
81
  {
162
-
163
82
  Unknown,
164
-
165
83
  Male,
166
-
167
84
  Female,
168
-
169
85
  }
170
86
 
171
-
172
-
173
87
  public class Item
174
-
175
88
  {
176
-
177
89
  public string Syouhinmei { get; set; }
178
-
179
90
  public int Price { get; set; }
180
-
181
91
  public double Tax { get; set; }
182
-
183
92
  public Gender Sex { get; set; }
184
-
185
93
  }
186
94
 
187
-
188
-
189
95
  public partial class MainWindow : Window
190
-
191
96
  {
192
-
193
97
  public Dictionary<Gender, string> GenderDict { get; }
194
-
195
98
  public ObservableCollection<Item> Items { get; }
196
99
 
197
-
198
-
199
100
  public MainWindow()
200
-
201
101
  {
202
-
203
102
  InitializeComponent();
204
-
205
103
  DataContext = this;
206
104
 
207
-
208
-
209
105
  GenderDict = new Dictionary<Gender, string>
210
-
211
106
  {
212
-
213
107
  { Gender.Unknown, "?" },
214
-
215
108
  { Gender.Male, "♂" },
216
-
217
109
  { Gender.Female, "♀" },
218
-
219
110
  };
220
111
 
221
-
222
-
223
112
  Items = new ObservableCollection<Item>
224
-
225
113
  {
226
-
227
114
  new Item { Syouhinmei = "化粧品", Price = 1900, Tax = 10, Sex = Gender.Female },
228
-
229
115
  new Item { Syouhinmei = "洗剤", Price = 500, Tax = 10, Sex = Gender.Male },
230
-
231
116
  new Item { Syouhinmei = "パン", Price = 800, Tax = 8, Sex = Gender.Male },
232
-
233
117
  new Item { Syouhinmei = "牛乳", Price = 800, Tax = 8 },
234
-
235
118
  };
236
-
237
119
  }
238
120
 
239
-
240
-
241
121
  private void Button_Click(object sender, RoutedEventArgs e)
242
-
243
122
  {
244
-
245
123
  foreach(var irem in Items)
246
-
247
124
  {
248
-
249
125
  Debug.WriteLine($"Price:{irem.Price}, Sex:{irem.Sex}");
250
-
251
126
  }
252
-
253
127
  Debug.WriteLine("");
254
-
255
128
  }
256
-
257
129
  }
258
-
259
130
  }
260
-
261
131
  ```
262
132
 
263
-
264
-
265
133
  コード側から`Item`の中身を変更する場合は、ikarimameさんの言うように`INotifyPropertyChanged`の実装が必要です。