Is there a simple possibility to check if the DataGrid is currently in EditMode (Without to subscribe to BeginningEdit and CellEditEnding)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With