Mamad RN
Mamad RN

Reputation: 710

How can i add mouse double click to data grid items wpf

In windows form application our data grid view has lots of event like row mouse double click or row click and extra ...

But in WPF i cant find these event . How can i add row mouse double click to my user control that has a data grid in it

i did it with some bad way that i used data grid mouse double click event and some bug happened in this way but i want know simple and standard way

i also add double click event to data grid items in row_load event but it seems make my program slow if data grid has big source

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseDoubleClick += new MouseButtonEventHandler(Row_MouseDoubleClick);
}

Upvotes: 2

Views: 3531

Answers (2)

ColinE
ColinE

Reputation: 70132

You can handle the double-click on the DataGrid element, then look at the event source to find the row and column that was clicked:

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject dep = (DependencyObject)e.OriginalSource;

    // iteratively traverse the visual tree
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    if (dep is DataGridColumnHeader)
    {
        DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
        // do something
    }

    if (dep is DataGridCell)
    {
        DataGridCell cell = dep as DataGridCell;
        // do something
    }
}

I describe this in detail in this blog post that I wrote.

Upvotes: 7

Mamad RN
Mamad RN

Reputation: 710

The Colin answer was really good and worked ... i also use this code and this was helpfull for me and want to share it to other.

private void myGridView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DependencyObject dep = (DependencyObject)e.OriginalSource;

            // iteratively traverse the visual tree
            while ((dep != null) && !(dep is DataGridRow) && !(dep is DataGridColumnHeader))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
                return;

            if (dep is DataGridRow)
            {
                DataGridRow row = dep as DataGridRow;
               //here i can cast the row to that class i want 
            }
        }

As i want know when all row clicked i used this

Upvotes: 0

Related Questions