I am using DataGrid and trying to change the default background color for every other row (odd number rows have yellow color while even number rows have white color) since the color is too similar to the highlight color.
Here is my code:
MyDataGrid.LoadingRow += delegate(object sender, DataGridRowEventArgs e)
{
var currentRowContext = e.Row.DataContext;
if (currentRowContext.GetType()
.GetProperty("OBJECTID")
.GetValue(currentRowContext, null)
.ToString()]) % 2 == 0)
{
e.Row.Background = new SolidColorBrush(Colors.White);
}
else
{
e.Row.Background = new SolidColorBrush(
new Color() {
R = 235,
G = 235,
B = 0,
A = 60
});
};
MyDataGrid.UnloadingRow += delegate(object sender, DataGridRowEventArgs e)
{
e.Row.Background = null;
};
It shows up correctly at first, but after I click any header to sort the records, the color messed up (i.e., not every other row have the same color). I find setting this DataGrid row color is really trick and don’t know if anybody already resolved this issue. How can I keep the alternating colors when sorting records?
Is this what you’re trying to achieve ? (meaning every second row gets own color). There’s an implemented alternation code in most relevant wpf controls.