views:

270

answers:

1

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

+1  A: 

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.

HCL
Great, thanks, for my opinion you should post this in a connection to Microsoft and post here a link so user can vote for it to be implemented out-the-box in the upcoming version.
Shimmy
I would suggest to replace the DependendencyProperty obj parameters with DataGrid dg, so the attached property can be only applied to objects of type DataGrid.
Shimmy
You're right - I've changed it.
HCL
How can I make sure that the handler of this behaviour is reached before other handlers declared in XAML etc.?
Shimmy
I found it, it's simple, I am using the PreparingCellForEdit event, which is raised after the BeginningEdit event.
Shimmy