Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:12:46+00:00 2026-05-24T05:12:46+00:00

I’ve written code to change the foreground property of a DataGrid cell if the

  • 0

I’ve written code to change the foreground property of a DataGrid cell if the row containing that cell meets a given rule (let’s say its text must have the value “Incomplete”). I can make this work fairly easy by catching the LoadingRow event in code behind and writing my logic there, but I feel like this is not a very elegant MVVM implementation. Here’s the code:

// Sets the foreground color of th 5th cell to red if the text in the cell corresponds 
// to a value specified in the ViewModel.
private void dgProfile_LoadingRow(object sender, DataGridRowEventArgs e)
    {

        this.dgProfile.SelectedIndex = e.Row.GetIndex();
        DataGridColumn column = this.dgProfile.Columns[4];
        FrameworkElement fe = column.GetCellContent(e.Row);
        FrameworkElement result = GetParent(fe, typeof(DataGridCell));
        if (result != null)
        {
            DataGridCell cell = (DataGridCell)result;
            if (((TextBlock)cell.Content).Text == (this.DataContext as ProfileViewModel).strIncompleteActivityStatus) cell.Foreground = new SolidColorBrush(Colors.Red);
            else cell.Foreground = new SolidColorBrush(Colors.Black);
        }

    }
    private FrameworkElement GetParent(FrameworkElement child, Type targetType)
    {
        object parent = child.Parent;
        if (parent != null)
        {
            if (parent.GetType() == targetType)
            {
                return (FrameworkElement)parent;
            }
            else
            {
                return GetParent((FrameworkElement)parent, targetType);
            }
        }
        return null;
    }

Can someone tell me if there’s a better way to implement this using the MVVM Light toolkit, perhaps through RelayCommand and some clever data binding?

Thanks in advance for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:12:46+00:00Added an answer on May 24, 2026 at 5:12 am

    You can define a template column and bind the foreground property to a value converter that returns the appropriate SolidColorBrush.

    For example:

    <data:DataGrid.Columns>
         <data:DataGridTemplateColumn>
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding MyProperty}"
                                       Foreground="{Binding MyProperty, Converter={StaticResource MyConverter}}"/>
    
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>
    </data:DataGrid.Columns>
    

    And the converter:

    public class MyConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                string propertyValue = (string)value;
    
                if (propertyValue == strIncompleteActivityStatus)
                    return new SolidColorBrush(Colors.Red);
                else
                    return new SolidColorBrush(Colors.Black);
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    More details here:

    DataGrid cell color based on cell value

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.