locked
How to Highlight a selected selected Row RRS feed

  • Question

  • Hi all,

    I would like to change the background color for the selected/active row in the datagrid(Ex. search Data Screen), now it's grey and I want it to be yellow.

    I have tried the cellPaint function, but I just can't get it to work.

    Anny help will be appreciated!

    Thanx,

    Marius

    Friday, February 21, 2014 12:21 PM

Answers

  • Hi Marius, 

    There are 2 ways I can think of doing this - (both untested)

    The first way is to simply use the SelectionChanged event of the datagrid and add code similar to the following to the handler

    DataGrid d = (DataGrid)sender;
    DataGridRow r = (DataGridRow)d.Columns[0].GetCellContent(e.AddedItems).Parent;
    r.Background = new SolidColorBrush(Colors.Yellow);
    DataGridRow r2 = (DataGridRow)d.Columns[0].GetCellContent(e.RemovedItems).Parent;
    r2.Background = new SolidColorBrush(Colors.White);

    The other possible alternative is to add databinding to the BackGround property of the row. The Binding could point to the datagrid's SelectedItem property, and would need an IValueConverter which could compare the Row.DataContext which is passed as a parameter. If SelectedItem.Equals(Row.DataContext) == true then return the Colors.Yellow.

    The bindings would have to be added to each row though, which isn't straightforward in some cases.

    Regards

    Orange

    • Marked as answer by Angie Xu Friday, February 28, 2014 2:39 AM
    Friday, February 21, 2014 10:39 PM

All replies

  • Hi Marius, 

    There are 2 ways I can think of doing this - (both untested)

    The first way is to simply use the SelectionChanged event of the datagrid and add code similar to the following to the handler

    DataGrid d = (DataGrid)sender;
    DataGridRow r = (DataGridRow)d.Columns[0].GetCellContent(e.AddedItems).Parent;
    r.Background = new SolidColorBrush(Colors.Yellow);
    DataGridRow r2 = (DataGridRow)d.Columns[0].GetCellContent(e.RemovedItems).Parent;
    r2.Background = new SolidColorBrush(Colors.White);

    The other possible alternative is to add databinding to the BackGround property of the row. The Binding could point to the datagrid's SelectedItem property, and would need an IValueConverter which could compare the Row.DataContext which is passed as a parameter. If SelectedItem.Equals(Row.DataContext) == true then return the Colors.Yellow.

    The bindings would have to be added to each row though, which isn't straightforward in some cases.

    Regards

    Orange

    • Marked as answer by Angie Xu Friday, February 28, 2014 2:39 AM
    Friday, February 21, 2014 10:39 PM
  • Hi all,

    I finally made it (thanks to Marius).

    My code:

    private void Arbeitstag_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                DataGrid d = (DataGrid)sender;
                if (e.AddedItems.Count > 0 && d.Columns[0].GetCellContent(e.AddedItems[0]) != null)
                {
                    DataGridCellsPresenter r = (DataGridCellsPresenter)((DataGridCell)d.Columns[0].GetCellContent(e.AddedItems[0]).Parent).Parent;
                    r.Background = new SolidColorBrush(Color.FromArgb(255, (byte)207, (byte)236, (byte)248));
                }
                if (e.RemovedItems.Count > 0 && d.Columns[0].GetCellContent(e.RemovedItems[0]) != null)
                {
                    DataGridCellsPresenter r2 = (DataGridCellsPresenter)((DataGridCell)d.Columns[0].GetCellContent(e.RemovedItems[0]).Parent).Parent;
                    r2.Background = new SolidColorBrush(Colors.Transparent);
                }
    }

    Wednesday, December 21, 2016 1:43 PM