0
votes

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

  • mondaycolor, etc. <-- bind style to this

  • items observablecollection <-- data in grid is from this

    • quantities
1
I think you don't want to have anything related to the view in your ViewModel, e.g. the colors of your cells. Insted you can define them in the view using triggers or a binding with a converter. - franssu
Can you suggest a way to do this for my situation? - Richard
Your binding works for me. Do you see any binding error messages in VS output window? - Clemens
Ah, yes! Errors. Of course, my colours are in the viewmodel but not in the observablecollection items that make up the grid. How did I not spot that?! - Richard
Richard, go ahead for @franssu's converter approach. That saves you a lot of properties in your model/item class. - Clemens

1 Answers

1
votes

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