回答編集履歴
2
見直しキャンペーン中
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
[【WPF】Dictonaryの値をコンボボックスに表示できない、クラスを参照したい](https://teratail.com/questions/345835#reply-475188)
|
25
25
|
|
26
26
|
MainWindow
|
27
|
-
```x
|
27
|
+
```xml
|
28
28
|
<Window
|
29
29
|
x:Class="Qjru5bj6b9xz7g1.MainWindow"
|
30
30
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
@@ -63,7 +63,7 @@
|
|
63
63
|
</DataGrid>
|
64
64
|
</Window>
|
65
65
|
```
|
66
|
-
```
|
66
|
+
```cs
|
67
67
|
using System.Windows;
|
68
68
|
using System.Windows.Controls;
|
69
69
|
|
@@ -96,7 +96,7 @@
|
|
96
96
|
```
|
97
97
|
|
98
98
|
ChildWindow
|
99
|
-
```x
|
99
|
+
```xml
|
100
100
|
<Window
|
101
101
|
x:Class="Qjru5bj6b9xz7g1.ChildWindow"
|
102
102
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
@@ -150,7 +150,7 @@
|
|
150
150
|
</ItemsControl>
|
151
151
|
</Window>
|
152
152
|
```
|
153
|
-
```
|
153
|
+
```cs
|
154
154
|
using System.Windows;
|
155
155
|
|
156
156
|
namespace Qjru5bj6b9xz7g1
|
@@ -169,7 +169,7 @@
|
|
169
169
|
```
|
170
170
|
|
171
171
|
ViewModel
|
172
|
-
```
|
172
|
+
```cs
|
173
173
|
using System.Collections.ObjectModel;
|
174
174
|
using System.Windows;
|
175
175
|
using System.Windows.Input;
|
1
追加削除の例追加
test
CHANGED
@@ -29,6 +29,7 @@
|
|
29
29
|
x:Class="Qjru5bj6b9xz7g1.MainWindow"
|
30
30
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
31
31
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
32
|
+
x:Name="Root"
|
32
33
|
Title="MainWindow"
|
33
34
|
Width="800"
|
34
35
|
Height="450">
|
@@ -39,26 +40,56 @@
|
|
39
40
|
<DataGridTextColumn Binding="{Binding Top}" Header="Top" />
|
40
41
|
<DataGridTextColumn Binding="{Binding Width}" Header="Width" />
|
41
42
|
<DataGridTextColumn Binding="{Binding Height}" Header="Height" />
|
43
|
+
<DataGridTemplateColumn Header="追加">
|
44
|
+
<DataGridTemplateColumn.CellTemplate>
|
45
|
+
<DataTemplate>
|
46
|
+
<!-- 追加ボタンで一部コピーしたアイテムを追加します(コードビハインドでの例) -->
|
47
|
+
<Button Click="Add_Click" Content="+" />
|
48
|
+
</DataTemplate>
|
49
|
+
</DataGridTemplateColumn.CellTemplate>
|
50
|
+
</DataGridTemplateColumn>
|
51
|
+
<DataGridTemplateColumn Header="削除">
|
52
|
+
<DataGridTemplateColumn.CellTemplate>
|
53
|
+
<DataTemplate>
|
54
|
+
<!-- 削除ボタンでアイテムを削除します(MVVMコマンドでの例) -->
|
55
|
+
<Button
|
56
|
+
Command="{Binding DataContext.DelCommand, Source={x:Reference Root}}"
|
57
|
+
CommandParameter="{Binding}"
|
58
|
+
Content="-" />
|
59
|
+
</DataTemplate>
|
60
|
+
</DataGridTemplateColumn.CellTemplate>
|
61
|
+
</DataGridTemplateColumn>
|
42
62
|
</DataGrid.Columns>
|
43
63
|
</DataGrid>
|
44
64
|
</Window>
|
45
65
|
```
|
46
66
|
```C#
|
47
67
|
using System.Windows;
|
68
|
+
using System.Windows.Controls;
|
48
69
|
|
49
70
|
namespace Qjru5bj6b9xz7g1
|
50
71
|
{
|
51
72
|
public partial class MainWindow : Window
|
52
73
|
{
|
74
|
+
private readonly ViewModel vm = new ViewModel();
|
75
|
+
|
76
|
+
|
53
77
|
public MainWindow()
|
54
78
|
{
|
55
79
|
InitializeComponent();
|
56
80
|
|
57
|
-
var vm = new ViewModel();
|
58
81
|
DataContext = vm;
|
59
|
-
|
60
|
-
// DataContextに同じvmを入れればいい
|
61
82
|
new ChildWindow { DataContext = vm, }.Show();
|
83
|
+
}
|
84
|
+
|
85
|
+
private void Add_Click(object sender, RoutedEventArgs e)
|
86
|
+
{
|
87
|
+
if (((Button)sender).DataContext is Item item)
|
88
|
+
{
|
89
|
+
//クリックした行と部分的に同じものを、1つ下の行に挿入
|
90
|
+
var i = vm.Items.IndexOf(item);
|
91
|
+
vm.Items.Insert(i + 1, new Item { Title = item.Title, Source = item.Source, });
|
92
|
+
}
|
62
93
|
}
|
63
94
|
}
|
64
95
|
}
|
@@ -71,10 +102,17 @@
|
|
71
102
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
72
103
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
73
104
|
xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors"
|
105
|
+
x:Name="Root"
|
74
106
|
Title="ChildWindow"
|
75
107
|
Width="800"
|
76
108
|
Height="450">
|
77
|
-
<ItemsControl ItemsSource="{Binding Items}">
|
109
|
+
<ItemsControl Background="White" ItemsSource="{Binding Items}">
|
110
|
+
<ItemsControl.ContextMenu>
|
111
|
+
<ContextMenu>
|
112
|
+
<!-- 何もないところの右クリックメニューで新規アイテムを追加します(コードビハインドでの例) -->
|
113
|
+
<MenuItem Click="Add_Click" Header="追加" />
|
114
|
+
</ContextMenu>
|
115
|
+
</ItemsControl.ContextMenu>
|
78
116
|
<ItemsControl.ItemsPanel>
|
79
117
|
<ItemsPanelTemplate>
|
80
118
|
<Canvas />
|
@@ -83,6 +121,15 @@
|
|
83
121
|
<ItemsControl.ItemTemplate>
|
84
122
|
<DataTemplate>
|
85
123
|
<Grid>
|
124
|
+
<Grid.ContextMenu>
|
125
|
+
<ContextMenu>
|
126
|
+
<!-- アイテムの右クリックメニューでアイテムを削除します(MVVMコマンドでの例) -->
|
127
|
+
<MenuItem
|
128
|
+
Command="{Binding DataContext.DelCommand, Source={x:Reference Root}}"
|
129
|
+
CommandParameter="{Binding}"
|
130
|
+
Header="削除" />
|
131
|
+
</ContextMenu>
|
132
|
+
</Grid.ContextMenu>
|
86
133
|
<StackPanel>
|
87
134
|
<StackPanel.RenderTransform>
|
88
135
|
<TranslateTransform X="{Binding Left}" Y="{Binding Top}" />
|
@@ -111,6 +158,12 @@
|
|
111
158
|
public partial class ChildWindow : Window
|
112
159
|
{
|
113
160
|
public ChildWindow() => InitializeComponent();
|
161
|
+
|
162
|
+
private void Add_Click(object sender, RoutedEventArgs e)
|
163
|
+
{
|
164
|
+
// DataContextがViewModelのはずなのでキャストして使用する
|
165
|
+
((ViewModel)DataContext).Items.Add(new Item());
|
166
|
+
}
|
114
167
|
}
|
115
168
|
}
|
116
169
|
```
|
@@ -118,18 +171,34 @@
|
|
118
171
|
ViewModel
|
119
172
|
```C#
|
120
173
|
using System.Collections.ObjectModel;
|
174
|
+
using System.Windows;
|
175
|
+
using System.Windows.Input;
|
121
176
|
using CommunityToolkit.Mvvm.ComponentModel;
|
177
|
+
using CommunityToolkit.Mvvm.Input;
|
122
178
|
|
123
179
|
namespace Qjru5bj6b9xz7g1
|
124
180
|
{
|
125
181
|
public class ViewModel
|
126
182
|
{
|
127
183
|
public ObservableCollection<Item> Items { get; } = new ObservableCollection<Item>();
|
184
|
+
public ICommand DelCommand { get; }
|
128
185
|
|
129
186
|
public ViewModel()
|
130
187
|
{
|
131
188
|
Items.Add(new Item { Title = "TN8001", Left = 0, Top = 0, Width = 200, Source = "https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail.jpg", });
|
132
189
|
Items.Add(new Item { Title = "nya-3", Source = "https://www.gravatar.com/avatar/4d48208fd97fc8a0d8d0765a11592aa5?d=identicon", });
|
190
|
+
|
191
|
+
DelCommand = new RelayCommand<Item>(Del);
|
192
|
+
}
|
193
|
+
|
194
|
+
private void Del(Item item)
|
195
|
+
{
|
196
|
+
// MVVM的にはよろしくないが...
|
197
|
+
var r = MessageBox.Show("削除します。いいですか?", "", MessageBoxButton.YesNo, MessageBoxImage.Information);
|
198
|
+
if (r == MessageBoxResult.Yes)
|
199
|
+
{
|
200
|
+
Items.Remove(item); // 当該アイテムを削除
|
201
|
+
}
|
133
202
|
}
|
134
203
|
}
|
135
204
|
|
@@ -155,7 +224,7 @@
|
|
155
224
|
}
|
156
225
|
}
|
157
226
|
```
|
158
|
-
![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2022-0
|
227
|
+
![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-01/ca25eb78-050f-4f65-bef4-f3c8da81e558.png)
|
159
228
|
|
160
229
|
---
|
161
230
|
|