Avatar of James Young jyoung@blueberries.com
James Young jyoung@blueberries.com
 asked on

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;
}
* WPF* xamlC#

Avatar of undefined
Last Comment
James Young jyoung@blueberries.com

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
James Young jyoung@blueberries.com

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
James Young jyoung@blueberries.com

ASKER
Figured it out myself.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes