Richard
Richard

Reputation: 277

Why is my WPF datagrid background colour not binding?

I'm using an MVVM approach. I've got a datagrid with columns for the days of the week. I ned to highlight the current day with a shaded background. Everything else in the viewmodel is displaying properly so it's binding in general. However, the shading is never applied and if I put breakpoints in my properties, the breakpoints are never hit. I'm doing something stupid but can't spot what.

Here's my code for the colours in the viewmodel:

  public Brush SundayColor { get { return GetBrushColorForWeekday(DayOfWeek.Sunday); } }

    public Brush MondayColor { get { return GetBrushColorForWeekday(DayOfWeek.Monday); } }

    public Brush TuesdayColor { get { return GetBrushColorForWeekday(DayOfWeek.Tuesday); } }

    public Brush WednesdayColor { get { return GetBrushColorForWeekday(DayOfWeek.Wednesday); } }

    public Brush ThursdayColor { get { return GetBrushColorForWeekday(DayOfWeek.Thursday); } }

    public Brush FridayColor { get { return GetBrushColorForWeekday(DayOfWeek.Friday); } }

    public Brush SaturdayColor { get { return GetBrushColorForWeekday(DayOfWeek.Saturday); } }

    private Brush GetBrushColorForWeekday(DayOfWeek dayOfWeek)
    {
        return dayOfWeek == CurrentDate.DayOfWeek ? Brushes.AliceBlue : Brushes.White;
    }

For the grid XAML I'm using the following. To make it brief I've shown one column definition only but the other six are similar:

   <DataGridTextColumn Header="Mon" Width="33" Binding="{Binding MondayQuantity,NotifyOnTargetUpdated=True}">
        <DataGridTextColumn.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Background" Value="{Binding MondayColor}"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>

I know the styles work as if I change the binding to a fixed colour I see the chosen colour. So why aren't my bindings working?

EDIT

I was being stupid! The colour properties are in my viewmodel directly, not in the items that the grid is bound to.

Is it possible to bind the grid styles to items from the viewmodel that are outside of the observablecolleciton the grid is bound to? I suppose, "one level up" is what I'm after:

viewmodel

Upvotes: 0

Views: 463

Answers (1)

franssu
franssu

Reputation: 2430

I'm used to using converters so... I'm thinking about something like that (psoeudo-code)

Xaml:

<Setter Property="Background" Value="{Binding Parameter="Monday",
                                      Converter={StaticResource currentDayToColorCOnverter}}"/>

Converter:

class CurrentyDayToColorConverter : IValueConverter
{
    public object Convert(blablabla, .., object parameter)
    {
        if (CurrentDate.DayOfWeek == parameter) // may not be exactly that but you get the idea
        {
             return Brushes.AliceBlue;
        }
        else
        { 
              return Brushes.White
        }
    }
}

Upvotes: 1

Related Questions