I have a DataGrid and need to set individual background colors on some cells and change the color of the cell on selection. Changing the color on selection works well and if try setting the background color without binding it works, too. I assume my binding is wrong.
Therefore i used this code in xaml
<Style TargetType="{x:Type DataGridCell}" x:Key="NumberCell">
<Style.Setters>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor},Path=StatusColor}"></Setter>
</Style.Setters>
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True" >
<Setter Property="Background" Value="{StaticResource LoudBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource LoudBrush}" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Trigger>
</Style.Triggers>
</Style>
my code for populating the datagrid:
private void AddColumns(DataGrid dataGrid, IEnumerable<User> Users)
{
var columnsAmount = dataGrid.Columns.Count;
if(columnsAmount > 1)
{
var dayColumnKepper = dataGrid.Columns[0];
dataGrid.Columns.Clear();
dataGrid.Columns.Insert(0, dayColumnKepper);
}
foreach(var user in Users)
{
var column = new DataGridTextColumn();
column.Binding = new Binding(string.Format("UserWorkplan[{0}].Appointment.Type", user.Id));
column.Header = user.Username;
column.CellStyle = (Style)Resources["NumberCell"];
dataGrid.Columns.Insert(1, column);
}
}
public IDictionary<int, CalenderWorkplanEntry> UserWorkplan { get; set; }
StatusColor should be the color of the individual row, but the color is always the datagrid default color.
public class CalenderWorkplanEntry
{
public string Fullname { get; set; }
public int UserId { get; set; }
public string StatusColor { get; set; }
public WorkPlanAppointment Appointment { get; set; }
}