Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 227
HCL Avatar asked Jul 14 '10 15:07

HCL


2 Answers

It seems you can also get this information from the items view, namely this works:

IEditableCollectionView itemsView = stateGrid.Items;
if (itemsView.IsAddingNew || itemsView.IsEditingItem)
{
    stateGrid.CommitEdit(DataGridEditingUnit.Row, true);
}

I have not confirmed this but most likely you could get these flags in a viewmodel if your bound collection provides an IEditableCollectionView.

like image 146
flolim Avatar answered Sep 23 '22 07:09

flolim


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<DataGridBeginningEditEventArgs>(dataGrid_BeginningEdit);
                dataGrid.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(dataGrid_CellEditEnding);
            } else {
                dataGrid.BeginningEdit -= new EventHandler<DataGridBeginningEditEventArgs>(dataGrid_BeginningEdit);
                dataGrid.CellEditEnding -= new EventHandler<DataGridCellEditEndingEventArgs>(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:

<DataGrid local:DataGridIsInEditModeTracker.ProcessIsInEditMode="True" ..  other properties ..>

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.

like image 27
HCL Avatar answered Sep 19 '22 07:09

HCL