I have the below simplified code to color certain rows of my DataGrid. I would like to do this task programmatically and not through XAML.
public IEnumerable<System.Windows.Controls.DataGridRow> GetDataGridRows(System.Windows.Controls.DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as System.Windows.Controls.DataGridRow;
if (null != row) yield return row;
}
}
public void color()
{
var rows = GetDataGridRows(dg1);
foreach (DataGridRow r in rows)
{
//DataRowView rv = (DataRowView)r.Item;
//remove code for simplicity
r.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(100, 100, 100, 100));
}
}
Doing this does not change the background of the row.