Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my WPF datagrid background colour not binding?
    primarykey
    data
    text
    <p>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.</p> <p>Here's my code for the colours in the viewmodel:</p> <pre><code> 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; } </code></pre> <p>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:</p> <pre><code> &lt;DataGridTextColumn Header="Mon" Width="33" Binding="{Binding MondayQuantity,NotifyOnTargetUpdated=True}"&gt; &lt;DataGridTextColumn.CellStyle&gt; &lt;Style TargetType="DataGridCell"&gt; &lt;Setter Property="Background" Value="{Binding MondayColor}"/&gt; &lt;Setter Property="Foreground" Value="Black"/&gt; &lt;/Style&gt; &lt;/DataGridTextColumn.CellStyle&gt; &lt;/DataGridTextColumn&gt; </code></pre> <p>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?</p> <h2>EDIT</h2> <p>I was being stupid! The colour properties are in my viewmodel directly, not in the items that the grid is bound to.</p> <p>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:</p> <p>viewmodel</p> <ul> <li><p>mondaycolor, etc. &lt;-- bind style to this</p></li> <li><p>items observablecollection &lt;-- data in grid is from this</p> <ul> <li>quantities</li> </ul></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload