Changing the foreground color of a DataGrid row based on property

Hello Experts, hoping someone could show me what I'm doing wrong. Note: my code does run and I experience no errors AND code execution breaks in the Value Converter, but does not change the color of the cell as anticipated.

Overview: I am loading some pallets into a DataGrid. If Quality Control is completed I want the text to be black. If Quality Control is needed, then I want the foreground to be Red. Also because I have multiple bindings going on, I decided against using the ElementStyle and instead am trying to use a DataGridTemplate.

Any help would be appreciated.

Warm regards,
James

// Color Converter ---------------------------------------------------

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace MBGQuality.Converters
{
    class ColorConverter : IValueConverter
    {
        public static readonly IValueConverter Instance = new ColorConverter();
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool IsQCDone = (bool)value;

            if (IsQCDone == true)
                return Brushes.Black;

            if (IsQCDone == false)
                return Brushes.Red;

            return Brushes.Transparent;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

// LookupPallet.xaml ---------------------------------------------------

<DataGrid Name="dgPallets" HorizontalAlignment="Left" Height="207" Margin="15,165,0,0" VerticalAlignment="Top" Width="528" AutoGenerateColumns="False" HorizontalScrollBarVisibility="Disabled" SelectionMode="Single" IsReadOnly="True" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
      
      <DataGridTemplateColumn Header="Pallet" Width="125" >
            <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                        <TextBlock Text="{Binding Path= pPallet}" FontSize="16" Foreground="{Binding IsQCDone, Converter={StaticResource ColorConverter},RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
                  </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
      
      <DataGridTextColumn Binding="{Binding Path= Product}" Header="Product" Width="250" FontSize="16" />    
      <DataGridTextColumn Binding="{Binding Path= dDate, StringFormat='MM/dd/yyyy'}" Header="Date" Width="150" FontSize="16"/>              
</DataGrid.Columns>
</DataGrid>

// LookupPallet.xaml.cs ---------------------------------------------------

private bool _isQCDone;
public bool IsQCDone
{
      get { return _isQCDone; }

      set
      {
            _isQCDone = value;
            OnPropertyChanged("IsQCDone");                
      }
}

private void LookupPallet_Loaded(object sender, RoutedEventArgs e)
{
      IsQCDone = false; // <-- setting property here manually for now

      this.Left = 0;
      this.Top = 5;
      
      dpDate.Text = ptq.TodayDate.ToShortDateString();
}

private void GetData()
{
      if (dpDate == null)
            return;
      
      if (ptq.Pallet.Type == 2)          
            dsPallets = ptc.pkr_Packed_List(2, 0, DateTime.Parse(dpDate.Text), ptq.Month);      
      
      if (ptq.Pallet.Type == 6)            
            dsPallets = ptc.pkr_Receipts_toQC_get();    

      // set up view using dataset and set itemsource to dataview
      dvPallets = new DataView(dsPallets.Tables[0]);
      dgPallets.ItemsSource = dvPallets;
}
James Young jyoung@blueberries.comAsked:
Who is Participating?
 
James Young jyoung@blueberries.comAuthor Commented:
Decided to go with a DataGrid.RowStyle and DataTrigger for my solution. While I only wanted the Pallet number to be red, targeting a single column, our team is fine coloring the row. Works like a charm.
0
 
James Young jyoung@blueberries.comAuthor Commented:
Figured it out myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.