I am trying to change datagrid cell color by using a converter which will get the cell as parameter because i will need the cell and row to choose the color of cell.The data is dynamic therefore i don't have any model.Problem is the converter does'nt get hit while loading data.
private void RDataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{
foreach (var dataGridColumn in RDataGrid.Columns)
{
var textColumn = dataGridColumn as DataGridTextColumn;
if (textColumn == null) continue;
textColumn.ElementStyle = FindResource("gridElementStyle") as Style;
textColumn.EditingElementStyle = FindResource("gridEditElementStyle") as Style;
textColumn.CellStyle = FindResource("gridCellStyle") as Style;
}
}
This is the style which is binded to datagrid.
<Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background"
Value="{Binding .
, RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}
,Converter={StaticResource ColorConverter}
}" />
</Style>
Converter:
public class ColorConverter : IValueConverter
{
//private string[,] yourarray = new string[100, 100];
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DataGridCell cell = (DataGridCell)value;
return Brushes.Red;
//int x = cell.Column.DisplayIndex;
//var parent = VisualTreeHelper.GetParent(cell);
//while (parent != null && parent.GetType() != typeof(DataGridRow))
//{
// parent = VisualTreeHelper.GetParent(parent);
//}
//int y = ((DataGridRow)parent).GetIndex();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}