Arne
Arne

Reputation: 11

Datagrid cell background color binding

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; }

}

Upvotes: 0

Views: 1361

Answers (1)

Sheridan
Sheridan

Reputation: 69987

First things first... please do yourself a favour and do not manipulate UI objects in the code behind. The correct way to bind to a collection control is to implement the INotifyPropertyChanged interface, define a public ObservableCollection<T> property and to data bind it to the DataGrid.ItemsSource property:

<DataGrid ItemsSource="{Binding Items}" />

Next, to get rid of the default selection colours, add this into your Resources section:

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</DataGrid.Resources>

You can play around with these colours to suit your requirements, or just leave them as Transparent so that they do not hide your Trigger colours.

Please let me know if I have misunderstood your problem.

Upvotes: 1

Related Questions