troubleshooting Question

Changing the foreground color of a DataGrid row based on property

Avatar of James Young jyoung@blueberries.com
James Young jyoung@blueberries.com asked on
* WPF* xamlC#
2 Comments1 Solution162 ViewsLast Modified:
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;
}
ASKER CERTIFIED SOLUTION
James Young jyoung@blueberries.comSr. Applications Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros