回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,133 +1,133 @@
|
|
1
|
-
`ComboBox`の選択肢は個々のアイテム(`DataGrid`の行)の1項目であって、全く別のものであるはずです。
|
2
|
-
|
3
|
-
なので`Gender`と`Item`に分けました。
|
4
|
-
`Gender`はクラスでもいいのですが、扱いにくいので列挙型にしました。
|
5
|
-
列挙型と表示文字列の対応付けはいろいろありますが、元コードと近い`Dictionary`方式にしました。
|
6
|
-
|
7
|
-
```
|
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>-->
|
41
|
-
|
42
|
-
<DataGridTemplateColumn Header="コンボボックス列">
|
43
|
-
<DataGridTemplateColumn.CellTemplate>
|
44
|
-
<DataTemplate>
|
45
|
-
<ComboBox
|
46
|
-
DisplayMemberPath="Value"
|
47
|
-
ItemsSource="{Binding DataContext.GenderDict, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
|
48
|
-
SelectedValue="{Binding Sex, UpdateSourceTrigger=PropertyChanged}"
|
49
|
-
SelectedValuePath="Key" />
|
50
|
-
</DataTemplate>
|
51
|
-
</DataGridTemplateColumn.CellTemplate>
|
52
|
-
</DataGridTemplateColumn>
|
53
|
-
|
54
|
-
<DataGridComboBoxColumn
|
55
|
-
DisplayMemberPath="Value"
|
56
|
-
Header="DataGridComboBoxColumn"
|
57
|
-
ItemsSource="{Binding Source={StaticResource GenderDictSource}}"
|
58
|
-
SelectedValueBinding="{Binding Sex}"
|
59
|
-
SelectedValuePath="Key" />
|
60
|
-
</DataGrid.Columns>
|
61
|
-
</DataGrid>
|
62
|
-
|
63
|
-
<Button
|
64
|
-
HorizontalAlignment="Left"
|
65
|
-
VerticalAlignment="Bottom"
|
66
|
-
Click="Button_Click"
|
67
|
-
Content="値の確認" />
|
68
|
-
</Grid>
|
69
|
-
</Window>
|
70
|
-
```
|
71
|
-
|
72
|
-
```
|
73
|
-
using System.Collections.Generic;
|
74
|
-
using System.Collections.ObjectModel;
|
75
|
-
using System.Diagnostics;
|
76
|
-
using System.Windows;
|
77
|
-
|
78
|
-
namespace Questions302281
|
79
|
-
{
|
80
|
-
public enum Gender
|
81
|
-
{
|
82
|
-
Unknown,
|
83
|
-
Male,
|
84
|
-
Female,
|
85
|
-
}
|
86
|
-
|
87
|
-
public class Item
|
88
|
-
{
|
89
|
-
public string Syouhinmei { get; set; }
|
90
|
-
public int Price { get; set; }
|
91
|
-
public double Tax { get; set; }
|
92
|
-
public Gender Sex { get; set; }
|
93
|
-
}
|
94
|
-
|
95
|
-
public partial class MainWindow : Window
|
96
|
-
{
|
97
|
-
public Dictionary<Gender, string> GenderDict { get; }
|
98
|
-
public ObservableCollection<Item> Items { get; }
|
99
|
-
|
100
|
-
public MainWindow()
|
101
|
-
{
|
102
|
-
InitializeComponent();
|
103
|
-
DataContext = this;
|
104
|
-
|
105
|
-
GenderDict = new Dictionary<Gender, string>
|
106
|
-
{
|
107
|
-
{ Gender.Unknown, "?" },
|
108
|
-
{ Gender.Male, "♂" },
|
109
|
-
{ Gender.Female, "♀" },
|
110
|
-
};
|
111
|
-
|
112
|
-
Items = new ObservableCollection<Item>
|
113
|
-
{
|
114
|
-
new Item { Syouhinmei = "化粧品", Price = 1900, Tax = 10, Sex = Gender.Female },
|
115
|
-
new Item { Syouhinmei = "洗剤", Price = 500, Tax = 10, Sex = Gender.Male },
|
116
|
-
new Item { Syouhinmei = "パン", Price = 800, Tax = 8, Sex = Gender.Male },
|
117
|
-
new Item { Syouhinmei = "牛乳", Price = 800, Tax = 8 },
|
118
|
-
};
|
119
|
-
}
|
120
|
-
|
121
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
122
|
-
{
|
123
|
-
foreach(var irem in Items)
|
124
|
-
{
|
125
|
-
Debug.WriteLine($"Price:{irem.Price}, Sex:{irem.Sex}");
|
126
|
-
}
|
127
|
-
Debug.WriteLine("");
|
128
|
-
}
|
129
|
-
}
|
130
|
-
}
|
131
|
-
```
|
132
|
-
|
1
|
+
`ComboBox`の選択肢は個々のアイテム(`DataGrid`の行)の1項目であって、全く別のものであるはずです。
|
2
|
+
|
3
|
+
なので`Gender`と`Item`に分けました。
|
4
|
+
`Gender`はクラスでもいいのですが、扱いにくいので列挙型にしました。
|
5
|
+
列挙型と表示文字列の対応付けはいろいろありますが、元コードと近い`Dictionary`方式にしました。
|
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>-->
|
41
|
+
|
42
|
+
<DataGridTemplateColumn Header="コンボボックス列">
|
43
|
+
<DataGridTemplateColumn.CellTemplate>
|
44
|
+
<DataTemplate>
|
45
|
+
<ComboBox
|
46
|
+
DisplayMemberPath="Value"
|
47
|
+
ItemsSource="{Binding DataContext.GenderDict, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
|
48
|
+
SelectedValue="{Binding Sex, UpdateSourceTrigger=PropertyChanged}"
|
49
|
+
SelectedValuePath="Key" />
|
50
|
+
</DataTemplate>
|
51
|
+
</DataGridTemplateColumn.CellTemplate>
|
52
|
+
</DataGridTemplateColumn>
|
53
|
+
|
54
|
+
<DataGridComboBoxColumn
|
55
|
+
DisplayMemberPath="Value"
|
56
|
+
Header="DataGridComboBoxColumn"
|
57
|
+
ItemsSource="{Binding Source={StaticResource GenderDictSource}}"
|
58
|
+
SelectedValueBinding="{Binding Sex}"
|
59
|
+
SelectedValuePath="Key" />
|
60
|
+
</DataGrid.Columns>
|
61
|
+
</DataGrid>
|
62
|
+
|
63
|
+
<Button
|
64
|
+
HorizontalAlignment="Left"
|
65
|
+
VerticalAlignment="Bottom"
|
66
|
+
Click="Button_Click"
|
67
|
+
Content="値の確認" />
|
68
|
+
</Grid>
|
69
|
+
</Window>
|
70
|
+
```
|
71
|
+
|
72
|
+
```cs
|
73
|
+
using System.Collections.Generic;
|
74
|
+
using System.Collections.ObjectModel;
|
75
|
+
using System.Diagnostics;
|
76
|
+
using System.Windows;
|
77
|
+
|
78
|
+
namespace Questions302281
|
79
|
+
{
|
80
|
+
public enum Gender
|
81
|
+
{
|
82
|
+
Unknown,
|
83
|
+
Male,
|
84
|
+
Female,
|
85
|
+
}
|
86
|
+
|
87
|
+
public class Item
|
88
|
+
{
|
89
|
+
public string Syouhinmei { get; set; }
|
90
|
+
public int Price { get; set; }
|
91
|
+
public double Tax { get; set; }
|
92
|
+
public Gender Sex { get; set; }
|
93
|
+
}
|
94
|
+
|
95
|
+
public partial class MainWindow : Window
|
96
|
+
{
|
97
|
+
public Dictionary<Gender, string> GenderDict { get; }
|
98
|
+
public ObservableCollection<Item> Items { get; }
|
99
|
+
|
100
|
+
public MainWindow()
|
101
|
+
{
|
102
|
+
InitializeComponent();
|
103
|
+
DataContext = this;
|
104
|
+
|
105
|
+
GenderDict = new Dictionary<Gender, string>
|
106
|
+
{
|
107
|
+
{ Gender.Unknown, "?" },
|
108
|
+
{ Gender.Male, "♂" },
|
109
|
+
{ Gender.Female, "♀" },
|
110
|
+
};
|
111
|
+
|
112
|
+
Items = new ObservableCollection<Item>
|
113
|
+
{
|
114
|
+
new Item { Syouhinmei = "化粧品", Price = 1900, Tax = 10, Sex = Gender.Female },
|
115
|
+
new Item { Syouhinmei = "洗剤", Price = 500, Tax = 10, Sex = Gender.Male },
|
116
|
+
new Item { Syouhinmei = "パン", Price = 800, Tax = 8, Sex = Gender.Male },
|
117
|
+
new Item { Syouhinmei = "牛乳", Price = 800, Tax = 8 },
|
118
|
+
};
|
119
|
+
}
|
120
|
+
|
121
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
122
|
+
{
|
123
|
+
foreach(var irem in Items)
|
124
|
+
{
|
125
|
+
Debug.WriteLine($"Price:{irem.Price}, Sex:{irem.Sex}");
|
126
|
+
}
|
127
|
+
Debug.WriteLine("");
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
```
|
132
|
+
|
133
133
|
コード側から`Item`の中身を変更する場合は、ikarimameさんの言うように`INotifyPropertyChanged`の実装が必要です。
|