回答編集履歴
1
見直しキャンペーン中
    
        answer	
    CHANGED
    
    | 
         @@ -1,107 +1,107 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            > 列3の入力が確定した時点で 列4、5の値を参照して
         
     | 
| 
       2 
     | 
    
         
            -
            > 入力値が範囲内ならOK、範囲外ならエラーとして
         
     | 
| 
       3 
     | 
    
         
            -
            > そのセルからフォーカス移動をさせない
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
            はっきり言ってこのような操作妨害はどうかと思います(通常バリデーション(validation 値の検証)で、エラーが分かれば十分だと思いますが)
         
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
            しかしやりたい方もいるようですね^^;
         
     | 
| 
       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)
         
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
            `CellEditEnding`の時点ではまだ値が反映されていない(だからキャンセルできるわけですが)のが非常に面倒です。
         
     | 
| 
       12 
     | 
    
         
            -
            [DataGrid.CellEditEnding イベント (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagrid.celleditending)
         
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
            提示条件しか考慮していません(MaxをValue以下にしたらどうなるかとか)
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
            ``` 
     | 
| 
       17 
     | 
    
         
            -
            <Window
         
     | 
| 
       18 
     | 
    
         
            -
              x:Class="Questions358523.MainWindow"
         
     | 
| 
       19 
     | 
    
         
            -
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         
     | 
| 
       20 
     | 
    
         
            -
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         
     | 
| 
       21 
     | 
    
         
            -
              Width="800"
         
     | 
| 
       22 
     | 
    
         
            -
              Height="450">
         
     | 
| 
       23 
     | 
    
         
            -
              <Grid>
         
     | 
| 
       24 
     | 
    
         
            -
                <DataGrid
         
     | 
| 
       25 
     | 
    
         
            -
                  AutoGenerateColumns="False"
         
     | 
| 
       26 
     | 
    
         
            -
                  CellEditEnding="DataGrid_CellEditEnding"
         
     | 
| 
       27 
     | 
    
         
            -
                  ItemsSource="{Binding Items}">
         
     | 
| 
       28 
     | 
    
         
            -
                  <DataGrid.Columns>
         
     | 
| 
       29 
     | 
    
         
            -
                    <DataGridTextColumn
         
     | 
| 
       30 
     | 
    
         
            -
                      Width="200"
         
     | 
| 
       31 
     | 
    
         
            -
                      Binding="{Binding Name}"
         
     | 
| 
       32 
     | 
    
         
            -
                      Header="名前" />
         
     | 
| 
       33 
     | 
    
         
            -
                    <DataGridTextColumn
         
     | 
| 
       34 
     | 
    
         
            -
                      Width="100"
         
     | 
| 
       35 
     | 
    
         
            -
                      Binding="{Binding Value}"
         
     | 
| 
       36 
     | 
    
         
            -
                      Header="値" />
         
     | 
| 
       37 
     | 
    
         
            -
                    <DataGridTextColumn
         
     | 
| 
       38 
     | 
    
         
            -
                      Width="100"
         
     | 
| 
       39 
     | 
    
         
            -
                      Binding="{Binding Min}"
         
     | 
| 
       40 
     | 
    
         
            -
                      Header="最小値" />
         
     | 
| 
       41 
     | 
    
         
            -
                    <DataGridTextColumn
         
     | 
| 
       42 
     | 
    
         
            -
                      Width="100"
         
     | 
| 
       43 
     | 
    
         
            -
                      Binding="{Binding Max}"
         
     | 
| 
       44 
     | 
    
         
            -
                      Header="最大値" />
         
     | 
| 
       45 
     | 
    
         
            -
                  </DataGrid.Columns>
         
     | 
| 
       46 
     | 
    
         
            -
                </DataGrid>
         
     | 
| 
       47 
     | 
    
         
            -
              </Grid>
         
     | 
| 
       48 
     | 
    
         
            -
            </Window>
         
     | 
| 
       49 
     | 
    
         
            -
            ```
         
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
            ``` 
     | 
| 
       52 
     | 
    
         
            -
            using System;
         
     | 
| 
       53 
     | 
    
         
            -
            using System.Collections.ObjectModel;
         
     | 
| 
       54 
     | 
    
         
            -
            using System.Windows;
         
     | 
| 
       55 
     | 
    
         
            -
            using System.Windows.Controls;
         
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
            namespace Questions358523
         
     | 
| 
       58 
     | 
    
         
            -
            {
         
     | 
| 
       59 
     | 
    
         
            -
                public class Item
         
     | 
| 
       60 
     | 
    
         
            -
                {
         
     | 
| 
       61 
     | 
    
         
            -
                    public string Name { get; set; }
         
     | 
| 
       62 
     | 
    
         
            -
                    public int Value { get; set; }
         
     | 
| 
       63 
     | 
    
         
            -
                    public int Min { get; set; }
         
     | 
| 
       64 
     | 
    
         
            -
                    public int Max { get; set; }
         
     | 
| 
       65 
     | 
    
         
            -
                }
         
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
       67 
     | 
    
         
            -
                public partial class MainWindow : Window
         
     | 
| 
       68 
     | 
    
         
            -
                {
         
     | 
| 
       69 
     | 
    
         
            -
                    public ObservableCollection<Item> Items { get; } = new();
         
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
                    public MainWindow()
         
     | 
| 
       72 
     | 
    
         
            -
                    {
         
     | 
| 
       73 
     | 
    
         
            -
                        InitializeComponent();
         
     | 
| 
       74 
     | 
    
         
            -
                        DataContext = this;
         
     | 
| 
       75 
     | 
    
         
            -
             
     | 
| 
       76 
     | 
    
         
            -
                        Items.Add(new() { Name = "あああ", Min = 0, Max = 90, });
         
     | 
| 
       77 
     | 
    
         
            -
                        Items.Add(new() { Name = "いいい", Min = 0, Max = 91, });
         
     | 
| 
       78 
     | 
    
         
            -
                        Items.Add(new() { Name = "ううう", Min = 0, Max = 92, });
         
     | 
| 
       79 
     | 
    
         
            -
                    }
         
     | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
                    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
         
     | 
| 
       82 
     | 
    
         
            -
                    {
         
     | 
| 
       83 
     | 
    
         
            -
                        if (e.EditAction != DataGridEditAction.Commit) return;
         
     | 
| 
       84 
     | 
    
         
            -
                        if (sender is not DataGrid dataGrid) return;
         
     | 
| 
       85 
     | 
    
         
            -
                        if (e.EditingElement is not TextBox textBox) return;
         
     | 
| 
       86 
     | 
    
         
            -
                        if (e.Row.Item is not Item item) return;
         
     | 
| 
       87 
     | 
    
         
            -
                        if (e.Column.SortMemberPath != nameof(Item.Value)) return;
         
     | 
| 
       88 
     | 
    
         
            -
                        if (!int.TryParse(textBox.Text, out var value)) return;
         
     | 
| 
       89 
     | 
    
         
            -
             
     | 
| 
       90 
     | 
    
         
            -
                        if (item.Min <= value && value <= item.Max) return; // ok-
         
     | 
| 
       91 
     | 
    
         
            -
             
     | 
| 
       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)
         
     | 
| 
       93 
     | 
    
         
            -
                        e.Cancel = true;
         
     | 
| 
       94 
     | 
    
         
            -
                        dataGrid.Dispatcher.BeginInvoke((Action)(() =>
         
     | 
| 
       95 
     | 
    
         
            -
                        {
         
     | 
| 
       96 
     | 
    
         
            -
                            dataGrid.SelectedIndex = e.Row.GetIndex();
         
     | 
| 
       97 
     | 
    
         
            -
                            e.EditingElement.Focus();
         
     | 
| 
       98 
     | 
    
         
            -
                        }));
         
     | 
| 
       99 
     | 
    
         
            -
                    }
         
     | 
| 
       100 
     | 
    
         
            -
                }
         
     | 
| 
       101 
     | 
    
         
            -
            }
         
     | 
| 
       102 
     | 
    
         
            -
            ```
         
     | 
| 
       103 
     | 
    
         
            -
             
     | 
| 
       104 
     | 
    
         
            -
            C# 9.0です。9以前だと下記2つがエラーになると思われます。
         
     | 
| 
       105 
     | 
    
         
            -
            [ターゲットからの new 型推論 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/cheatsheet/ap_ver9/#target-typed-new)
         
     | 
| 
       106 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            > 列3の入力が確定した時点で 列4、5の値を参照して
         
     | 
| 
      
 2 
     | 
    
         
            +
            > 入力値が範囲内ならOK、範囲外ならエラーとして
         
     | 
| 
      
 3 
     | 
    
         
            +
            > そのセルからフォーカス移動をさせない
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            はっきり言ってこのような操作妨害はどうかと思います(通常バリデーション(validation 値の検証)で、エラーが分かれば十分だと思いますが)
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            しかしやりたい方もいるようですね^^;
         
     | 
| 
      
 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)
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            `CellEditEnding`の時点ではまだ値が反映されていない(だからキャンセルできるわけですが)のが非常に面倒です。
         
     | 
| 
      
 12 
     | 
    
         
            +
            [DataGrid.CellEditEnding イベント (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.datagrid.celleditending)
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            提示条件しか考慮していません(`Max`を`Value`以下にしたらどうなるかとか)
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            ```xml
         
     | 
| 
      
 17 
     | 
    
         
            +
            <Window
         
     | 
| 
      
 18 
     | 
    
         
            +
              x:Class="Questions358523.MainWindow"
         
     | 
| 
      
 19 
     | 
    
         
            +
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         
     | 
| 
      
 20 
     | 
    
         
            +
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         
     | 
| 
      
 21 
     | 
    
         
            +
              Width="800"
         
     | 
| 
      
 22 
     | 
    
         
            +
              Height="450">
         
     | 
| 
      
 23 
     | 
    
         
            +
              <Grid>
         
     | 
| 
      
 24 
     | 
    
         
            +
                <DataGrid
         
     | 
| 
      
 25 
     | 
    
         
            +
                  AutoGenerateColumns="False"
         
     | 
| 
      
 26 
     | 
    
         
            +
                  CellEditEnding="DataGrid_CellEditEnding"
         
     | 
| 
      
 27 
     | 
    
         
            +
                  ItemsSource="{Binding Items}">
         
     | 
| 
      
 28 
     | 
    
         
            +
                  <DataGrid.Columns>
         
     | 
| 
      
 29 
     | 
    
         
            +
                    <DataGridTextColumn
         
     | 
| 
      
 30 
     | 
    
         
            +
                      Width="200"
         
     | 
| 
      
 31 
     | 
    
         
            +
                      Binding="{Binding Name}"
         
     | 
| 
      
 32 
     | 
    
         
            +
                      Header="名前" />
         
     | 
| 
      
 33 
     | 
    
         
            +
                    <DataGridTextColumn
         
     | 
| 
      
 34 
     | 
    
         
            +
                      Width="100"
         
     | 
| 
      
 35 
     | 
    
         
            +
                      Binding="{Binding Value}"
         
     | 
| 
      
 36 
     | 
    
         
            +
                      Header="値" />
         
     | 
| 
      
 37 
     | 
    
         
            +
                    <DataGridTextColumn
         
     | 
| 
      
 38 
     | 
    
         
            +
                      Width="100"
         
     | 
| 
      
 39 
     | 
    
         
            +
                      Binding="{Binding Min}"
         
     | 
| 
      
 40 
     | 
    
         
            +
                      Header="最小値" />
         
     | 
| 
      
 41 
     | 
    
         
            +
                    <DataGridTextColumn
         
     | 
| 
      
 42 
     | 
    
         
            +
                      Width="100"
         
     | 
| 
      
 43 
     | 
    
         
            +
                      Binding="{Binding Max}"
         
     | 
| 
      
 44 
     | 
    
         
            +
                      Header="最大値" />
         
     | 
| 
      
 45 
     | 
    
         
            +
                  </DataGrid.Columns>
         
     | 
| 
      
 46 
     | 
    
         
            +
                </DataGrid>
         
     | 
| 
      
 47 
     | 
    
         
            +
              </Grid>
         
     | 
| 
      
 48 
     | 
    
         
            +
            </Window>
         
     | 
| 
      
 49 
     | 
    
         
            +
            ```
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
            ```cs
         
     | 
| 
      
 52 
     | 
    
         
            +
            using System;
         
     | 
| 
      
 53 
     | 
    
         
            +
            using System.Collections.ObjectModel;
         
     | 
| 
      
 54 
     | 
    
         
            +
            using System.Windows;
         
     | 
| 
      
 55 
     | 
    
         
            +
            using System.Windows.Controls;
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
            namespace Questions358523
         
     | 
| 
      
 58 
     | 
    
         
            +
            {
         
     | 
| 
      
 59 
     | 
    
         
            +
                public class Item
         
     | 
| 
      
 60 
     | 
    
         
            +
                {
         
     | 
| 
      
 61 
     | 
    
         
            +
                    public string Name { get; set; }
         
     | 
| 
      
 62 
     | 
    
         
            +
                    public int Value { get; set; }
         
     | 
| 
      
 63 
     | 
    
         
            +
                    public int Min { get; set; }
         
     | 
| 
      
 64 
     | 
    
         
            +
                    public int Max { get; set; }
         
     | 
| 
      
 65 
     | 
    
         
            +
                }
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                public partial class MainWindow : Window
         
     | 
| 
      
 68 
     | 
    
         
            +
                {
         
     | 
| 
      
 69 
     | 
    
         
            +
                    public ObservableCollection<Item> Items { get; } = new();
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                    public MainWindow()
         
     | 
| 
      
 72 
     | 
    
         
            +
                    {
         
     | 
| 
      
 73 
     | 
    
         
            +
                        InitializeComponent();
         
     | 
| 
      
 74 
     | 
    
         
            +
                        DataContext = this;
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                        Items.Add(new() { Name = "あああ", Min = 0, Max = 90, });
         
     | 
| 
      
 77 
     | 
    
         
            +
                        Items.Add(new() { Name = "いいい", Min = 0, Max = 91, });
         
     | 
| 
      
 78 
     | 
    
         
            +
                        Items.Add(new() { Name = "ううう", Min = 0, Max = 92, });
         
     | 
| 
      
 79 
     | 
    
         
            +
                    }
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
         
     | 
| 
      
 82 
     | 
    
         
            +
                    {
         
     | 
| 
      
 83 
     | 
    
         
            +
                        if (e.EditAction != DataGridEditAction.Commit) return;
         
     | 
| 
      
 84 
     | 
    
         
            +
                        if (sender is not DataGrid dataGrid) return;
         
     | 
| 
      
 85 
     | 
    
         
            +
                        if (e.EditingElement is not TextBox textBox) return;
         
     | 
| 
      
 86 
     | 
    
         
            +
                        if (e.Row.Item is not Item item) return;
         
     | 
| 
      
 87 
     | 
    
         
            +
                        if (e.Column.SortMemberPath != nameof(Item.Value)) return;
         
     | 
| 
      
 88 
     | 
    
         
            +
                        if (!int.TryParse(textBox.Text, out var value)) return;
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
                        if (item.Min <= value && value <= item.Max) return; // ok-
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 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)
         
     | 
| 
      
 93 
     | 
    
         
            +
                        e.Cancel = true;
         
     | 
| 
      
 94 
     | 
    
         
            +
                        dataGrid.Dispatcher.BeginInvoke((Action)(() =>
         
     | 
| 
      
 95 
     | 
    
         
            +
                        {
         
     | 
| 
      
 96 
     | 
    
         
            +
                            dataGrid.SelectedIndex = e.Row.GetIndex();
         
     | 
| 
      
 97 
     | 
    
         
            +
                            e.EditingElement.Focus();
         
     | 
| 
      
 98 
     | 
    
         
            +
                        }));
         
     | 
| 
      
 99 
     | 
    
         
            +
                    }
         
     | 
| 
      
 100 
     | 
    
         
            +
                }
         
     | 
| 
      
 101 
     | 
    
         
            +
            }
         
     | 
| 
      
 102 
     | 
    
         
            +
            ```
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
            C# 9.0です。9以前だと下記2つがエラーになると思われます。
         
     | 
| 
      
 105 
     | 
    
         
            +
            [ターゲットからの new 型推論 - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/cheatsheet/ap_ver9/#target-typed-new)
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
       107 
107 
     | 
    
         
             
            [not パターン - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/datatype/patterns/?p=3#not-pattern)
         
     |