回答編集履歴

1

見直しキャンペーン中

2023/07/29 05:14

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,213 +1,107 @@
1
1
  > 列3の入力が確定した時点で 列4、5の値を参照して
2
-
3
2
  > 入力値が範囲内ならOK、範囲外ならエラーとして
4
-
5
3
  > そのセルからフォーカス移動をさせない
6
-
7
-
8
4
 
9
5
  はっきり言ってこのような操作妨害はどうかと思います(通常バリデーション(validation 値の検証)で、エラーが分かれば十分だと思いますが)
10
6
 
11
-
12
-
13
7
  しかしやりたい方もいるようですね^^;
14
-
15
8
  [c# - Keep cursor in cell after cancel in WPF DataGrid CellEditEnding event with MVVM - Stack Overflow](https://stackoverflow.com/questions/52981358/keep-cursor-in-cell-after-cancel-in-wpf-datagrid-celleditending-event-with-mvvm)
16
9
 
17
10
 
18
-
19
-
20
-
21
11
  `CellEditEnding`の時点ではまだ値が反映されていない(だからキャンセルできるわけですが)のが非常に面倒です。
22
-
23
12
  [DataGrid.CellEditEnding イベント (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagrid.celleditending)
24
13
 
14
+ 提示条件しか考慮していません(`Max`を`Value`以下にしたらどうなるかとか)
25
15
 
26
-
27
- 提示条件しか考慮していません(MaxをValue以下にしたらどうなるかとか)
28
-
29
-
30
-
31
- ```xaml
16
+ ```xml
32
-
33
17
  <Window
34
-
35
18
  x:Class="Questions358523.MainWindow"
36
-
37
19
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
-
39
20
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
40
-
41
21
  Width="800"
42
-
43
22
  Height="450">
44
-
45
23
  <Grid>
46
-
47
24
  <DataGrid
48
-
49
25
  AutoGenerateColumns="False"
50
-
51
26
  CellEditEnding="DataGrid_CellEditEnding"
52
-
53
27
  ItemsSource="{Binding Items}">
54
-
55
28
  <DataGrid.Columns>
56
-
57
29
  <DataGridTextColumn
58
-
59
30
  Width="200"
60
-
61
31
  Binding="{Binding Name}"
62
-
63
32
  Header="名前" />
64
-
65
33
  <DataGridTextColumn
66
-
67
34
  Width="100"
68
-
69
35
  Binding="{Binding Value}"
70
-
71
36
  Header="値" />
72
-
73
37
  <DataGridTextColumn
74
-
75
38
  Width="100"
76
-
77
39
  Binding="{Binding Min}"
78
-
79
40
  Header="最小値" />
80
-
81
41
  <DataGridTextColumn
82
-
83
42
  Width="100"
84
-
85
43
  Binding="{Binding Max}"
86
-
87
44
  Header="最大値" />
88
-
89
45
  </DataGrid.Columns>
90
-
91
46
  </DataGrid>
92
-
93
47
  </Grid>
94
-
95
48
  </Window>
96
-
97
49
  ```
98
50
 
99
-
100
-
101
- ```C#
51
+ ```cs
102
-
103
52
  using System;
104
-
105
53
  using System.Collections.ObjectModel;
106
-
107
54
  using System.Windows;
108
-
109
55
  using System.Windows.Controls;
110
56
 
111
-
112
-
113
57
  namespace Questions358523
114
-
115
58
  {
116
-
117
59
  public class Item
118
-
119
60
  {
120
-
121
61
  public string Name { get; set; }
122
-
123
62
  public int Value { get; set; }
124
-
125
63
  public int Min { get; set; }
126
-
127
64
  public int Max { get; set; }
128
-
129
65
  }
130
66
 
131
-
132
-
133
67
  public partial class MainWindow : Window
134
-
135
68
  {
136
-
137
69
  public ObservableCollection<Item> Items { get; } = new();
138
70
 
139
-
140
-
141
71
  public MainWindow()
142
-
143
72
  {
144
-
145
73
  InitializeComponent();
146
-
147
74
  DataContext = this;
148
75
 
149
-
150
-
151
76
  Items.Add(new() { Name = "あああ", Min = 0, Max = 90, });
152
-
153
77
  Items.Add(new() { Name = "いいい", Min = 0, Max = 91, });
154
-
155
78
  Items.Add(new() { Name = "ううう", Min = 0, Max = 92, });
156
-
157
79
  }
158
80
 
159
-
160
-
161
81
  private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
162
-
163
82
  {
164
-
165
83
  if (e.EditAction != DataGridEditAction.Commit) return;
166
-
167
84
  if (sender is not DataGrid dataGrid) return;
168
-
169
85
  if (e.EditingElement is not TextBox textBox) return;
170
-
171
86
  if (e.Row.Item is not Item item) return;
172
-
173
87
  if (e.Column.SortMemberPath != nameof(Item.Value)) return;
174
-
175
88
  if (!int.TryParse(textBox.Text, out var value)) return;
176
-
177
-
178
89
 
179
90
  if (item.Min <= value && value <= item.Max) return; // ok-
180
91
 
181
-
182
-
183
92
  // [c# - Keep cursor in cell after cancel in WPF DataGrid CellEditEnding event with MVVM - Stack Overflow](https://stackoverflow.com/questions/52981358/keep-cursor-in-cell-after-cancel-in-wpf-datagrid-celleditending-event-with-mvvm)
184
-
185
93
  e.Cancel = true;
186
-
187
94
  dataGrid.Dispatcher.BeginInvoke((Action)(() =>
188
-
189
95
  {
190
-
191
96
  dataGrid.SelectedIndex = e.Row.GetIndex();
192
-
193
97
  e.EditingElement.Focus();
194
-
195
98
  }));
196
-
197
99
  }
198
-
199
100
  }
200
-
201
101
  }
202
-
203
102
  ```
204
103
 
205
-
206
-
207
104
  C# 9.0です。9以前だと下記2つがエラーになると思われます。
208
-
209
105
  [ターゲットからの new 型推論 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/cheatsheet/ap_ver9/#target-typed-new)
210
106
 
211
-
212
-
213
107
  [not パターン - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/datatype/patterns/?p=3#not-pattern)