回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,79 +1,79 @@
|
|
1
|
-
解決されたようですが、閲覧者向けにタイトルを回収しておきます。
|
2
|
-
|
3
|
-
本来`ListView`と`DataGrid`は役割が違います。
|
4
|
-
表示するだけなら`ListView`でいいですし、項目の編集をするなら`DataGrid`です。
|
5
|
-
|
6
|
-
```
|
7
|
-
<Window
|
8
|
-
x:Class="Questions314032.MainWindow"
|
9
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
10
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
11
|
-
Width="800"
|
12
|
-
Height="450">
|
13
|
-
<Grid>
|
14
|
-
<ListView ItemsSource="{Binding People}">
|
15
|
-
<ListView.View>
|
16
|
-
<GridView>
|
17
|
-
<GridViewColumn DisplayMemberBinding="{Binding Id}" Header="ID" />
|
18
|
-
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="名前" />
|
19
|
-
<GridViewColumn Header="削除">
|
20
|
-
<GridViewColumn.CellTemplate>
|
21
|
-
<DataTemplate>
|
22
|
-
<Button Content="削除" Click="Button_Click" />
|
23
|
-
</DataTemplate>
|
24
|
-
</GridViewColumn.CellTemplate>
|
25
|
-
</GridViewColumn>
|
26
|
-
</GridView>
|
27
|
-
</ListView.View>
|
28
|
-
</ListView>
|
29
|
-
</Grid>
|
30
|
-
</Window>
|
31
|
-
```
|
32
|
-
|
33
|
-
```
|
34
|
-
using System.Collections.ObjectModel;
|
35
|
-
using System.Windows;
|
36
|
-
using System.Windows.Controls;
|
37
|
-
|
38
|
-
namespace Questions314032
|
39
|
-
{
|
40
|
-
public class Person
|
41
|
-
{
|
42
|
-
public int Id { get; }
|
43
|
-
public string Name { get; }
|
44
|
-
public Person(int id, string name) => (Id, Name) = (id, name);
|
45
|
-
}
|
46
|
-
|
47
|
-
public partial class MainWindow : Window
|
48
|
-
{
|
49
|
-
public ObservableCollection<Person> People { get; }
|
50
|
-
public MainWindow()
|
51
|
-
{
|
52
|
-
InitializeComponent();
|
53
|
-
DataContext = this;
|
54
|
-
|
55
|
-
People = new()
|
56
|
-
{
|
57
|
-
new(1, "田中"),
|
58
|
-
new(2, "佐々木"),
|
59
|
-
};
|
60
|
-
}
|
61
|
-
|
62
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
63
|
-
{
|
64
|
-
if (sender is Button button)
|
65
|
-
{
|
66
|
-
People.Remove((Person)button.DataContext);
|
67
|
-
}
|
68
|
-
}
|
69
|
-
}
|
70
|
-
}
|
71
|
-
```
|
72
|
-
|
73
|
-
---
|
74
|
-
|
75
|
-
特に指定がないので.NET 5.0で書きました^^
|
76
|
-
.NET Framework 4.8等では、new()でエラーが出るので型を書いてください。
|
77
|
-
|
78
|
-
[Target-typed new expressions - C# 9.0 specification proposals | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-new)
|
1
|
+
解決されたようですが、閲覧者向けにタイトルを回収しておきます。
|
2
|
+
|
3
|
+
本来`ListView`と`DataGrid`は役割が違います。
|
4
|
+
表示するだけなら`ListView`でいいですし、項目の編集をするなら`DataGrid`です。
|
5
|
+
|
6
|
+
```xml
|
7
|
+
<Window
|
8
|
+
x:Class="Questions314032.MainWindow"
|
9
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
10
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
11
|
+
Width="800"
|
12
|
+
Height="450">
|
13
|
+
<Grid>
|
14
|
+
<ListView ItemsSource="{Binding People}">
|
15
|
+
<ListView.View>
|
16
|
+
<GridView>
|
17
|
+
<GridViewColumn DisplayMemberBinding="{Binding Id}" Header="ID" />
|
18
|
+
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="名前" />
|
19
|
+
<GridViewColumn Header="削除">
|
20
|
+
<GridViewColumn.CellTemplate>
|
21
|
+
<DataTemplate>
|
22
|
+
<Button Content="削除" Click="Button_Click" />
|
23
|
+
</DataTemplate>
|
24
|
+
</GridViewColumn.CellTemplate>
|
25
|
+
</GridViewColumn>
|
26
|
+
</GridView>
|
27
|
+
</ListView.View>
|
28
|
+
</ListView>
|
29
|
+
</Grid>
|
30
|
+
</Window>
|
31
|
+
```
|
32
|
+
|
33
|
+
```cs
|
34
|
+
using System.Collections.ObjectModel;
|
35
|
+
using System.Windows;
|
36
|
+
using System.Windows.Controls;
|
37
|
+
|
38
|
+
namespace Questions314032
|
39
|
+
{
|
40
|
+
public class Person
|
41
|
+
{
|
42
|
+
public int Id { get; }
|
43
|
+
public string Name { get; }
|
44
|
+
public Person(int id, string name) => (Id, Name) = (id, name);
|
45
|
+
}
|
46
|
+
|
47
|
+
public partial class MainWindow : Window
|
48
|
+
{
|
49
|
+
public ObservableCollection<Person> People { get; }
|
50
|
+
public MainWindow()
|
51
|
+
{
|
52
|
+
InitializeComponent();
|
53
|
+
DataContext = this;
|
54
|
+
|
55
|
+
People = new()
|
56
|
+
{
|
57
|
+
new(1, "田中"),
|
58
|
+
new(2, "佐々木"),
|
59
|
+
};
|
60
|
+
}
|
61
|
+
|
62
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
63
|
+
{
|
64
|
+
if (sender is Button button)
|
65
|
+
{
|
66
|
+
People.Remove((Person)button.DataContext);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
```
|
72
|
+
|
73
|
+
---
|
74
|
+
|
75
|
+
特に指定がないので.NET 5.0で書きました^^
|
76
|
+
.NET Framework 4.8等では、new()でエラーが出るので型を書いてください。
|
77
|
+
|
78
|
+
[Target-typed new expressions - C# 9.0 specification proposals | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-new)
|
79
79
|
[ターゲットからの new 型推論 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/oo_construct.html#target-typed-new)
|