Silverlight Datagrid select on right click

后端 未结 5 1834
半阙折子戏
半阙折子戏 2021-01-05 16:34

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

5条回答
  •  悲&欢浪女
    2021-01-05 17:14

    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:

    
            
                
            
    
    

提交回复
热议问题