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
110
Changing Row Color Through Code
posted

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;
    }
 
}
Parents Reply Children