6

Is there a way for a right click event to select a row in toolkit datagrid?

I'm using toolkit context menu which works nicely, but the problem is, only left click is able to select rows, and I need right click to be able to do that if I want my context menu to work properly.

Any help is appreciated

5 Answers5

5

You can find a solution here.

Basically it goes like this:

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown);
}
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    dg.SelectedItem = ((sender) as DataGridRow).DataContext;
}
Koynov
  • 1,499
  • 1
  • 10
  • 19
  • It's too bad we have to use hacks like this, but I suppose it's better than nothing. Thanks –  Jun 15 '10 at 17:24
  • The MouseRightButtonDown event is available only from SL 4.0 . Is there any other solution for right click in SL 3.0 ? – vijaysylvester Jan 25 '12 at 10:18
4

He's a Behavior which will do the trick for you (inspired by this blog post):

public class SelectRowOnRightClickBehavior : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.MouseRightButtonDown += HandleRightButtonClick;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.MouseRightButtonDown += HandleRightButtonClick;
    }

    private void HandleRightButtonClick(object sender, MouseButtonEventArgs e)
    {
        var elementsUnderMouse = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), AssociatedObject);

        var row = elementsUnderMouse
            .OfType<DataGridRow>()
            .FirstOrDefault();

        if (row != null)
            AssociatedObject.SelectedItem = row.DataContext;
    }
}

Use it like this:

<sdk:DataGrid x:Name="DataGrid" Grid.Row="4" 
                  IsReadOnly="True" 
                  ItemsSource="{Binding MyItems}">
        <i:Interaction.Behaviors>
            <b:SelectRowOnRightClickBehavior/>
        </i:Interaction.Behaviors>
</sdk:DataGrid>
Samuel Jack
  • 32,712
  • 16
  • 118
  • 155
1

Thanks good idea. But the with UnloadingRow event could have been more effective had been specified.

private void dg_UnloadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown -= Row_MouseRightButtonDown;
}
Orkun
  • 11
  • 3
1

This open source project on Codeplex supports this behavior out of the box and does much more than this:

http://sl4popupmenu.codeplex.com/

Ziad
  • 1,036
  • 2
  • 21
  • 31
0

I tried a slightly different approach using the LoadingRow event in the DataGrid. I don't like using that particular event if I don't have to, but since I wasn't working with large amounts of data, it works out pretty well. The only thing I don't have in this sample is the command to use to perform the action. You could use a command on the DataContext object or some other mechanism.

    private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        var contextMenu = new ContextMenu();

        var deleteMenuItem = new MenuItem {Header = "Delete User"};

        contextMenu.Items.Add(deleteMenuItem);

        ContextMenuService.SetContextMenu(e.Row, contextMenu);

    }
e-rock
  • 141
  • 1
  • 8