Home Forums WPF controls Xceed DataGrid for WPF Responding to mouse double-click on row

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21720 |

    I have a data grid that is set to NavigationBehavior RowOnly. I want to allow the user to edit the row data when that row is double-clicked with the mouse (or double-tapped with a stylus) by displaying an editor window (not using inline editing).

    I can hook the MouseDoubleClick event on the datagrid, but how do I determine which row actually received the double click?

    Thanks,

    Scott.

    Imported from legacy forums. Posted by Scott (had 11354 views)

    Xceed Support
    Member
    Post count: 5658

    Somewhere in your Initialization Code, you should place this statement:

    <code>
    EventManager.RegisterClassHandler( typeof( DataRow ), DataRow.MouseDoubleClickEvent, new MouseButtonEventHandler( OnDataRowMouseDoubleClick ) );
    </code>

    Then, you can handle the event as any other RoutedEvent:

    <code>
    private void OnDataRowMouseDoubleClick( object sender, MouseButtonEventArgs e )
    {
    DataRow row = sender as DataRow;

    if(row != null)
    {
    MessageBox.Show( “DoubleClick on DataRow: ” + row.Cells[ 0 ].Content.ToString() );
    }
    }
    </code>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 964 views)

    User (Old forums)
    Member
    Post count: 23064

    That works. I was orignally trying to set this up in the XAML and declared a MouseDoubleClick=”OnDoubleClick” attribute on the DataGridControl.

    Imported from legacy forums. Posted by Scott (had 754 views)

    User (Old forums)
    Member
    Post count: 23064

    This approach works, but leaks memory. I was looking at this in clrprofiler and wpf will hold a reference to the handler long after it is needed.

    Is there a way I can unregister the event handler when I’m done with it.

    Thanks

    Imported from legacy forums. Posted by Murthy (had 1748 views)

    User (Old forums)
    Member
    Post count: 23064

    Has anyone a solution for how to remove the registered Class handler as I have a similar problem having used the Eventmanager.RegisterClasshandler to intercept the ToopTipOpeningEvent in each cell.

    Imported from legacy forums. Posted by Derek (had 2410 views)

    User (Old forums)
    Member
    Post count: 23064

    Nearly one year later and I have the same problem!

    I think it should not be soooo hard to listen to a Row Double Click in a Grid!?!?

    Imported from legacy forums. Posted by Christian (had 771 views)

    User (Old forums)
    Member
    Post count: 23064

    Ok, I did it with a style and an EventSetter.

    Seems to work….

    <xcdg:DataGridControl.Resources>
    <Style TargetType=”{x:Type xcdg:DataRow}”>
    <EventSetter Event=”MouseDoubleClick” Handler=”OnDataRowMouseDoubleClick”/>
    </Style>
    </xcdg:DataGridControl.Resources>

    Imported from legacy forums. Posted by Christian (had 4968 views)

    User (Old forums)
    Member
    Post count: 23064

    I had a similar issue that I need to listen to the MouseUp event of a DataRow. To avoid memory leak, I had to put EventManager.RegisterClassHandler inside the static constructor and use static method such as private static void OnDataRowMouseUp

    Imported from legacy forums. Posted by Mark (had 1304 views)

    User (Old forums)
    Member
    Post count: 23064

    Marcus , Yes, but if you have multiple DataGrids in your application then this is not the solution (they will use the same global event handler ). Any other idea?

    Imported from legacy forums. Posted by Aleksav (had 984 views)

    User (Old forums)
    Member
    Post count: 23064

    If you want to show a row in a edit window just do this:

    private void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        Xceed.Wpf.DataGrid.DataGridControl grid = sender as Xceed.Wpf.DataGrid.DataGridControl;
        if (grid.CurrentItem != null)
        {
           
    int id = Convert.ToInt16(((DataRowView)grid.CurrentItem)[“Id”]);
            string descr = Convert.ToString(((DataRowView)grid.CurrentItem)[“Descr”]);
            Window2 win = new Window2();
            win.Title = id.ToString() +
    ” – “ + descr;
            win.Show();
       
    }
    }

    Agnar

    Imported from legacy forums. Posted by Agnar (had 774 views)

    User (Old forums)
    Member
    Post count: 23064

    If I put EventManager.RegisterClassHandler() in the static constructor and use static method as a handler of DataRow.MouseDoubleClickEvent then how could I access the instance of DataGridControl since the sender is a DataRow one, not DataGridControl?

    I have to do this since there’s an internal state of DataGridControl (I’m inheriting of it) that I need to track based on that event.

    Of course, if I put the Event.RegisterClassHandler() in the non-static constructor then there should be no problem at all, but that’s not the case I’m asking for.

    Imported from legacy forums. Posted by mahara (had 736 views)

    Xceed Support
    Member
    Post count: 5658

    Aleksav,

      Then you should make sure to filter the Rows based on their DataGrid’s Instance or Name.

      DataGridControl gridControl = DataGridControl.GetParentDataGridControl( myRowInstance);

      //Check name of gridControl, or compare with known instances.

    Mahara, code above will give you the DataGridControl instance.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 795 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank, Marcus. I’ll give it a try.

    Imported from legacy forums. Posted by mahara (had 644 views)

    User (Old forums)
    Member
    Post count: 23064

    [quote user=”Marcus”]Aleksav,

      Then you should make sure to filter the Rows based on their DataGrid’s Instance or Name.

      DataGridControl gridControl = DataGridControl.GetParentDataGridControl( myRowInstance);

      //Check name of gridControl, or compare with known instances.

    Mahara, code above will give you the DataGridControl instance.[/quote]

    I think it works as I expected so far. Thanks again.

    Imported from legacy forums. Posted by mahara (had 1433 views)

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.