Code to check if a cell of a DataGrid is currently edited

后端 未结 4 564
孤街浪徒
孤街浪徒 2020-12-16 01:49

Is there a simple possibility to check if the DataGrid is currently in EditMode (Without to subscribe to BeginningEdit and CellEditEnding)

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 02:26

    Ok, I havent found a simple solution and no one pointed me to one. The following code can be used to add an attached property IsInEditMode to a DataGrid. Hope it helps someone:

    public class DataGridIsInEditModeTracker {
    
        public static bool GetIsInEditMode(DataGrid dataGrid) {
            return (bool)dataGrid.GetValue(IsInEditModeProperty);
        }
    
        private static void SetIsInEditMode(DataGrid dataGrid, bool value) {
            dataGrid.SetValue(IsInEditModePropertyKey, value);
        }
    
        private static readonly DependencyPropertyKey IsInEditModePropertyKey = DependencyProperty.RegisterAttachedReadOnly("IsInEditMode", typeof(bool), typeof(DataGridIsInEditModeTracker), new UIPropertyMetadata(false));
    
        public static readonly DependencyProperty IsInEditModeProperty = IsInEditModePropertyKey.DependencyProperty;
    
    
        public static bool GetProcessIsInEditMode(DataGrid dataGrid) {
            return (bool)dataGrid.GetValue(ProcessIsInEditModeProperty);
        }
    
        public static void SetProcessIsInEditMode(DataGrid dataGrid, bool value) {
            dataGrid.SetValue(ProcessIsInEditModeProperty, value);
        }
    
    
        public static readonly DependencyProperty ProcessIsInEditModeProperty =
            DependencyProperty.RegisterAttached("ProcessIsInEditMode", typeof(bool), typeof(DataGridIsInEditModeTracker), new FrameworkPropertyMetadata(false, delegate(DependencyObject d,DependencyPropertyChangedEventArgs e) {
    
                DataGrid dataGrid = d as DataGrid;
                if (null == dataGrid) {
                    throw new InvalidOperationException("ProcessIsInEditMode can only be used with instances of the DataGrid-class");
                }
                if ((bool)e.NewValue) {
                    dataGrid.BeginningEdit += new EventHandler(dataGrid_BeginningEdit);
                    dataGrid.CellEditEnding += new EventHandler(dataGrid_CellEditEnding);
                } else {
                    dataGrid.BeginningEdit -= new EventHandler(dataGrid_BeginningEdit);
                    dataGrid.CellEditEnding -= new EventHandler(dataGrid_CellEditEnding);
                }
            }));
    
        static void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {            
            SetIsInEditMode((DataGrid)sender,false);
        }
    
        static void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) {
            SetIsInEditMode((DataGrid)sender, true);
        }                  
    }
    

    To use it, set on the datagrid the ProcessIsInEditMode- property to true:

    
    

    Afer that you will have the IsInEditMode-property in sync with the mode of the DataGrid. If you want also the editing cell, change the code in BeginningEdit accoringly.

提交回复
热议问题