views:

206

answers:

2

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

+2  A: 

See http://stackoverflow.com/questions/3248023/code-to-check-if-a-cell-of-a-datagrid-is-currently-edited/3248345#3248345

HCL
But how do I determine if it's edited? isn't there a simpler way instead of implementing the attached property?
Shimmy
I have not seen and no one posted a solution to my other post. I don't think that there is a more simple solution.
HCL
OK. Anyway, the CurrentItem property returns the entity object, which doesn't help us, I think you can remove it from your answer.I translated your link to VB, thanks for posting it!
Shimmy
A: 

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
Shimmy