MattB
MattB

Reputation: 2241

Need to determine which grid a double-click came from?

I think this should be fairly simple, but I've been looking through the properties in the signature for the handler that I'm using and I don't see any way to suss out what I'm looking for.

I have a fairly simple WPF app with two DataGrid controls in the same window. I have a double click event defined in the XAML like so:

<DataGrid.ItemContainerStyle>
    <Style TargetType="DataGridRow">
        <EventSetter
            Event="MouseDoubleClick"
            Handler="Row_DoubleClick"/>
    </Style>
</DataGrid.ItemContainerStyle>

And in the code behind (do we call it that in WPF apps?) I have the Row_DoubleClick handler set up like so:

Private Sub Row_DoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)

Now the sub itself works fine and picks up the row that was double-clicked just fine. However, as I noted before I have two DataGrids that use this same sub for the double-click event. I realize one path might be to simply make two subs, but it seems like I should be able to use the one for both, and it's taking the exact same action in either case, just using the row from one DataGrid or the other.

It always defaults to the first, let's call it IncompleteGrid, if a row is selected even if the second DataGrid, let's call it CompleteGrid, is the the one being double clicked. I've been looking through the sender and e objects in debug mode, but I don't see any place or property I can check to see which grid the double-click is coming from.

Any ideas?

Upvotes: 0

Views: 414

Answers (3)

Rohit Vats
Rohit Vats

Reputation: 81253

You can get the parent dataGrid from row by using VisualTreeHelper. Have this private method on your code (code is in C#, hope you can get it convert to VB easily):

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    DataGridRow row = sender as DataGridRow;
    DataGrid senderDataGrid = FindAncestor<DataGrid>(row);
}

private T FindAncestor<T>(DependencyObject dependencyObject)
    where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindAncestor<T>(parent);
}

VB Version:

Private Sub Row_DoubleClick(sender As Object, e As MouseButtonEventArgs)
    Dim row As DataGridRow = TryCast(sender, DataGridRow)
    Dim senderDataGrid As DataGrid = FindAncestor(Of DataGrid)(row)
End Sub

Private Function FindAncestor(Of T As DependencyObject)(dependencyObject As DependencyObject) As T
    Dim parent = VisualTreeHelper.GetParent(dependencyObject)

    If parent Is Nothing Then
        Return Nothing
    End If

    Dim parentT = TryCast(parent, T)
    Return If(parentT, FindAncestor(Of T)(parent))
End Function

Upvotes: 1

Jashaszun
Jashaszun

Reputation: 9270

sender should be the grid that the double-click is coming from. (That's the meaning of sender -- the control that sent the event.)

You can cast sender to a DataGrid if you want to do specific stuff with it.

Edit: If sender is a DataGridRow instead of a DataGrid, then you could use this question to find the host DataGrid. (Using a RelativeSource or a CommandParameter seems to the accepted methods for this.)

Upvotes: 0

Yair Nevet
Yair Nevet

Reputation: 13003

This parameter should give you the information:

ByVal sender As System.Object

Upvotes: 0

Related Questions