Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
425
How can I select a row by right click?
posted

Hello,

I am using contextMenu in XamGrid. But I found if I right click at another row other than the selected row, I could not get the correct contextMenu.

 

So I would like to select the row when I right click a row. Is there any way to do it?

Parents
No Data
Reply
  • 425
    Verified Answer
    posted

    I have solved it myself.

    private void DataGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)

            {

                IEnumerable<UIElement> elementsUnderMouse = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), this);

                CellControl cellControl = elementsUnderMouse

                    .Where(uie => uie is CellControl)

                    .Cast<CellControl>()

                    .FirstOrDefault();

     

                if (cellControl != null && !DataGrid.SelectionSettings.SelectedRows.Contains(cellControl.Cell.Row))

                {

    DataGrid.SelectionSettings.SelectedRows.Clear();

                    var selection = new SelectedRowsCollection();

                    foreach (var row in DataGrid.Rows)

                    {

                       List<object> items=new List<object> { cellControl.Cell.Row.Data } ;

                        if ( items.Contains(row.Data))

                        {

                            selection.Add(row);

                        }

                    }

                    DataGrid.SelectionSettings.SelectedRows.AddRange(selection);                

                    DataGrid.ActiveCell = cellControl.Cell;

                }

            }

Children
No Data