I'm working on being able to sort rows in my grid by dragging and dropping them, and I'd like to be able to change the color of rows that I'm hovering over with the mouse so the user knows which row we're dropping the other one onto.
I know there's no mouseEnter event for the rows from the searches I did, so I'm able to grab the row I'm hovering over by doing a MouseMove event with the grid (which only triggers if you're dragging). This is the code I'm using, but the row color is never set, so I'm wondering how I'm supposed to be doing this:
private void xamGrid_MouseMove(object sender, MouseEventArgs e) { position = e.GetPosition(xamGrid); if(isDragging) { //get current row and change the BG color maybe var elements = VisualTreeHelper.FindElementsInHostCoordinates(position, xamGrid); CellsPanel cellsPanel = null; foreach (UIElement elem in elements) { cellsPanel = elem as CellsPanel; if (cellsPanel != null) break; } if(cellsPanel != null) { if (cellsPanel != CurrentCellsPanel) { CurrentCellsPanel = cellsPanel; originalBG = CurrentCellsPanel.Row.Control.Background; var lightGreen = new Color { R = 150, B = 150, G = 255 }; CurrentCellsPanel.Row.Control.Background = new SolidColorBrush(lightGreen); } } } else if(CurrentCellsPanel != null) { //reset cell panel BG CurrentCellsPanel.Row.Control.Background = originalBG; CurrentCellsPanel = null; } }
Hi,
I am attaching another sample that shows how you can drag drop the headers of the grid. I think it looks the way you desire it - you can distinguish the cell that is being dragged as well as the cell that is being hovered.
Regards,
Stefana
While I can't figure out how to change the color of a row, I found that just adding it to the SelectedRows collection at least highlights them as I move over with this code in the MouseMove function in my example:
if (isDragging) { var elements = VisualTreeHelper.FindElementsInHostCoordinates(position, this); CellsPanel cellsPanel = null; foreach (UIElement elem in elements) { cellsPanel = elem as CellsPanel; if (cellsPanel != null) break; } if (cellsPanel != null) { if (cellsPanel != CurrentCellsPanel) { CurrentCellsPanel = cellsPanel; xamGrid.SelectionSettings.SelectedRows.Clear(); xamGrid.SelectionSettings.SelectedRows.Add(xamGrid.Rows[CurrentCellsPanel.Row.Index]); } } } else if (CurrentCellsPanel != null) { CurrentCellsPanel = null; xamGrid.SelectionSettings.SelectedRows.Clear(); }
I really wish there was just a MouseEnter and MouseLeave event for the rows, though instead of this incredibly slow VisualTreeHelper.FindElementsInHostCoordinates function...
I know they aren't built-in, but is there any way to implement any mouse events for rows so I don't have to constantly do hit tests with the mouse?
Yeah, I'm using the Drag and Drop framework. I'm attaching the example I'm working with. The function that handles the row color change is "xamGrid_MouseMove". If you step through it, the row control gets found just fine, and it looks like it sets the color, but it never changes on the actual UI.
Are you using the Drag and Drop framework and if so, can you please send me a sample.
Thanks,