Link to home
Start Free TrialLog in
Avatar of James Young
James Young

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,

// 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">
      <DataGridTemplateColumn Header="Pallet" Width="125" >
                        <TextBlock Text="{Binding Path= pPallet}" FontSize="16" Foreground="{Binding IsQCDone, Converter={StaticResource ColorConverter},RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
      <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"/>              

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

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

            _isQCDone = value;

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)
      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;
Avatar of James Young
James Young

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Young
James Young


Figured it out myself.