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 i
He's a Behavior which will do the trick for you (inspired by this blog post):
public class SelectRowOnRightClickBehavior : Behavior
{
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()
.FirstOrDefault();
if (row != null)
AssociatedObject.SelectedItem = row.DataContext;
}
}
Use it like this: