回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,80 +1,80 @@
|
|
1
|
-
簡単にやるなら`IsReadOnly="False"`のまま、通常行を`BeginningEdit`でキャンセルすることですかね。
|
2
|
-
|
3
|
-
xamlだけ
|
4
|
-
|
5
|
-
```
|
6
|
-
<Window
|
7
|
-
x:Class="Questions316745.MainWindow"
|
8
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
9
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
10
|
-
Width="800"
|
11
|
-
Height="450">
|
12
|
-
<Grid>
|
13
|
-
<DataGrid
|
14
|
-
AutoGenerateColumns="False"
|
15
|
-
BeginningEdit="DataGrid_BeginningEdit"
|
16
|
-
ItemsSource="{Binding Items}">
|
17
|
-
<DataGrid.Columns>
|
18
|
-
<!--<DataGridTextColumn Binding="{Binding Id}" Header="ID" IsReadOnly="True" />-->
|
19
|
-
<DataGridTextColumn Binding="{Binding Id}" Header="ID" />
|
20
|
-
<DataGridTextColumn Binding="{Binding Name}" Header="名前" />
|
21
|
-
<DataGridTextColumn Binding="{Binding Description}" Header="説明" />
|
22
|
-
</DataGrid.Columns>
|
23
|
-
</DataGrid>
|
24
|
-
</Grid>
|
25
|
-
</Window>
|
26
|
-
```
|
27
|
-
|
28
|
-
```
|
29
|
-
using Prism.Mvvm;
|
30
|
-
using System.Collections.ObjectModel;
|
31
|
-
using System.Windows;
|
32
|
-
using System.Windows.Controls;
|
33
|
-
|
34
|
-
namespace Questions316745
|
35
|
-
{
|
36
|
-
public class Item : BindableBase
|
37
|
-
{
|
38
|
-
private int _Id;
|
39
|
-
public int Id { get => _Id; set => SetProperty(ref _Id, value); }
|
40
|
-
|
41
|
-
private string _Name;
|
42
|
-
public string Name { get => _Name; set => SetProperty(ref _Name, value); }
|
43
|
-
|
44
|
-
private string _Description;
|
45
|
-
public string Description { get => _Description; set => SetProperty(ref _Description, value); }
|
46
|
-
}
|
47
|
-
|
48
|
-
public partial class MainWindow : Window
|
49
|
-
{
|
50
|
-
public ObservableCollection<Item> Items { get; }
|
51
|
-
|
52
|
-
public MainWindow()
|
53
|
-
{
|
54
|
-
InitializeComponent();
|
55
|
-
|
56
|
-
Items = new ObservableCollection<Item>
|
57
|
-
{
|
58
|
-
new Item{ Id = 1, Name = "hoge", Description = "aaa", },
|
59
|
-
new Item{ Id = 2, Name = "fuga", Description = "bbb", },
|
60
|
-
new Item{ Id = 3, Name = "piyo", Description = "ccc", },
|
61
|
-
};
|
62
|
-
|
63
|
-
DataContext = this;
|
64
|
-
}
|
65
|
-
|
66
|
-
private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
|
67
|
-
{
|
68
|
-
// もうちょっとましな判定法あると思うw
|
69
|
-
if (e.Column.Header.ToString() == "ID" && !e.Row.IsNewItem)
|
70
|
-
e.Cancel = true;
|
71
|
-
}
|
72
|
-
}
|
73
|
-
}
|
74
|
-
```
|
75
|
-
`INotifyPropertyChanged`に[Prism.Core](https://www.nuget.org/packages/Prism.Core/8.0.0.1909?_src=template)を使用しました。
|
76
|
-
|
77
|
-
---
|
78
|
-
|
79
|
-
しかしこういった要件というのは、ユニークなID等の列ではないのですか?
|
1
|
+
簡単にやるなら`IsReadOnly="False"`のまま、通常行を`BeginningEdit`でキャンセルすることですかね。
|
2
|
+
|
3
|
+
xamlだけでやれないこともなさそうなんですが、面倒そうなので考えるのを諦めました^^;
|
4
|
+
|
5
|
+
```xml
|
6
|
+
<Window
|
7
|
+
x:Class="Questions316745.MainWindow"
|
8
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
9
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
10
|
+
Width="800"
|
11
|
+
Height="450">
|
12
|
+
<Grid>
|
13
|
+
<DataGrid
|
14
|
+
AutoGenerateColumns="False"
|
15
|
+
BeginningEdit="DataGrid_BeginningEdit"
|
16
|
+
ItemsSource="{Binding Items}">
|
17
|
+
<DataGrid.Columns>
|
18
|
+
<!--<DataGridTextColumn Binding="{Binding Id}" Header="ID" IsReadOnly="True" />-->
|
19
|
+
<DataGridTextColumn Binding="{Binding Id}" Header="ID" />
|
20
|
+
<DataGridTextColumn Binding="{Binding Name}" Header="名前" />
|
21
|
+
<DataGridTextColumn Binding="{Binding Description}" Header="説明" />
|
22
|
+
</DataGrid.Columns>
|
23
|
+
</DataGrid>
|
24
|
+
</Grid>
|
25
|
+
</Window>
|
26
|
+
```
|
27
|
+
|
28
|
+
```cs
|
29
|
+
using Prism.Mvvm;
|
30
|
+
using System.Collections.ObjectModel;
|
31
|
+
using System.Windows;
|
32
|
+
using System.Windows.Controls;
|
33
|
+
|
34
|
+
namespace Questions316745
|
35
|
+
{
|
36
|
+
public class Item : BindableBase
|
37
|
+
{
|
38
|
+
private int _Id;
|
39
|
+
public int Id { get => _Id; set => SetProperty(ref _Id, value); }
|
40
|
+
|
41
|
+
private string _Name;
|
42
|
+
public string Name { get => _Name; set => SetProperty(ref _Name, value); }
|
43
|
+
|
44
|
+
private string _Description;
|
45
|
+
public string Description { get => _Description; set => SetProperty(ref _Description, value); }
|
46
|
+
}
|
47
|
+
|
48
|
+
public partial class MainWindow : Window
|
49
|
+
{
|
50
|
+
public ObservableCollection<Item> Items { get; }
|
51
|
+
|
52
|
+
public MainWindow()
|
53
|
+
{
|
54
|
+
InitializeComponent();
|
55
|
+
|
56
|
+
Items = new ObservableCollection<Item>
|
57
|
+
{
|
58
|
+
new Item{ Id = 1, Name = "hoge", Description = "aaa", },
|
59
|
+
new Item{ Id = 2, Name = "fuga", Description = "bbb", },
|
60
|
+
new Item{ Id = 3, Name = "piyo", Description = "ccc", },
|
61
|
+
};
|
62
|
+
|
63
|
+
DataContext = this;
|
64
|
+
}
|
65
|
+
|
66
|
+
private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
|
67
|
+
{
|
68
|
+
// もうちょっとましな判定法あると思うw
|
69
|
+
if (e.Column.Header.ToString() == "ID" && !e.Row.IsNewItem)
|
70
|
+
e.Cancel = true;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
```
|
75
|
+
`INotifyPropertyChanged`に[Prism.Core](https://www.nuget.org/packages/Prism.Core/8.0.0.1909?_src=template)を使用しました。
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
しかしこういった要件というのは、ユニークなID等の列ではないのですか?
|
80
80
|
バリデーションをかけるなら既存行も編集可にしてもよいですし、自動採番なら新規行も編集させたくないですよね?
|