Is there a way to determine whether a WPF DataGrid is in edit mode / which row is currently edited?
EDIT Duplicate: http://stackoverflow.com/questions/3248023/code-to-check-if-a-cell-of-a-datagrid-is-currently-edited
Is there a way to determine whether a WPF DataGrid is in edit mode / which row is currently edited?
EDIT Duplicate: http://stackoverflow.com/questions/3248023/code-to-check-if-a-cell-of-a-datagrid-is-currently-edited
Here is the VB translation of HCL's answer (with some minor alterations), HTH:
Public Class DataGridEditModeBehavior
Public Shared Function GetIsInEditMode(element As DataGrid) As Boolean
If element Is Nothing Then Throw New ArgumentNullException("element")
Return element.GetValue(IsInEditModeProperty)
End Function
Private Shared Sub SetIsInEditMode(element As DataGrid, value As Boolean)
If element Is Nothing Then Throw New ArgumentNullException("element")
element.SetValue(IsInEditModePropertyKey, value)
End Sub
Public Shared ReadOnly IsInEditModeProperty As DependencyProperty =
IsInEditModePropertyKey.DependencyProperty
Private Shared ReadOnly IsInEditModePropertyKey As DependencyPropertyKey =
DependencyProperty.RegisterAttachedReadOnly(
"IsInEditMode", GetType(Boolean),
GetType(DataGridEditModeBehavior), New UIPropertyMetadata(False))
Public Shared Function GetProcessIsInEditMode(
element As DataGrid) As Boolean
Return element.GetValue(ProcessIsInEditModeProperty)
End Function
Public Shared Sub SetProcessIsInEditMode(element As DataGrid,
value As Boolean)
element.SetValue(ProcessIsInEditModeProperty, value)
End Sub
Public Shared ReadOnly ProcessIsInEditModeProperty =
DependencyProperty.RegisterAttached("ProcessIsInEditMode",
GetType(Boolean), GetType(DataGridEditModeBehavior),
New FrameworkPropertyMetadata(False,
New DependencyPropertyChangedEventHandler(
Sub(element As Object, e As DependencyPropertyChangedEventArgs)
Dim dg = DirectCast(element, DataGrid)
If dg Is Nothing Then Throw New ArgumentNullException("element")
If DirectCast(e.NewValue, Boolean) Then
AddHandler dg.BeginningEdit, AddressOf DataGrid_BeginningEdit
AddHandler dg.CellEditEnding, AddressOf DataGrid_CellEditEnding
Else
RemoveHandler dg.BeginningEdit, AddressOf DataGrid_BeginningEdit
RemoveHandler dg.CellEditEnding, AddressOf DataGrid_CellEditEnding
End If
End Sub)))
Private Shared Sub DataGrid_BeginningEdit(ByVal sender As Object,
ByVal e As DataGridBeginningEditEventArgs)
SetIsInEditMode(DirectCast(sender, DataGrid), True)
End Sub
Private Shared Sub DataGrid_CellEditEnding(ByVal sender As Object,
ByVal e As DataGridCellEditEndingEventArgs)
SetIsInEditMode(DirectCast(sender, DataGrid), False)
End Sub
End Class