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?
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;