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

  • Home
  • SEARCH
  • 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 8797613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:53:21+00:00 2026-06-13T23:53:21+00:00

Similar to WPF – help converting XAML binding expression to codebehind I am attempting

  • 0

Similar to WPF – help converting XAML binding expression to codebehind

I am attempting to use Binding to change the color of certain elements in a DataGridTextColumn. Because I need an arbitrary number of DataGrids in separate tabs, I am creating them iteratively in the codebehind. Here is my code for creating the column:

// create a value column
column = new DataGridTextColumn();
column.Binding = new Binding("Value");
BindingOperations.SetBinding(column, DataGridTextColumn.ForegroundProperty, new Binding("TextColor"));
listGrid.Columns.Add(column);

The Value binding works fine, but the TextColor property’s getter never gets called.

The grid’s ItemsSource property is set to a list of VariableWatcher objects, and here are some of its properties:

public bool Value
{
    get { return _variable.Value; }
}
// used to set DataGridTextColumn.Foreground
public Brush TextColor
{
    get
    {
        Color brushColor;
         if (_valueChanged)
            brushColor = Color.FromRgb(255, 0, 0);
        else
            brushColor = Color.FromRgb(0, 0, 0);
         return new SolidColorBrush(brushColor);
    }
}

VariableWatcher implements INotifyPropertyChanged as follows:

public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

In one of VariableWatcher’s methods, I have the following lines:

_valueChanged = true;
NotifyPropertyChanged("Value");
NotifyPropertyChanged("TextColor");

Stepping over the “Value” line activates a breakpoint in the Value getter, and the Value text is updated in the display. However, stepping over the “TextColor” line does NOT activate the breakpoint in the TextColor getter, and the text color does not change. Any idea what’s going on here?

EDIT: Here is the answer, thanks to Damascus. (I would have put this in a comment on his answer, but it wouldn’t format my code properly.) I added this to the XAML file:

<Window.Resources>
    <Style x:Key="BoundColorStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="{Binding TextColor}" />
    </Style>
</Window.Resources>

and in the C# code, I replaced the BindingOperations line with

column.ElementStyle = this.FindResource("BoundColorStyle") as Style;
  • 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-06-13T23:53:21+00:00Added an answer on June 13, 2026 at 11:53 pm

    Workaround for this:

    Create a style in your resources, looking like that:

    <Style x:Key="MyStyle" TargetType="{x:Type TextBlock}">
      <Setter Property="Foreground" Value="{Binding TextColor}" />
    </Style>
    

    And set this style in the ElementStyle property of your DataGridColumn, should be something like that in your code:

    column = new DataGridTextColumn();
    column.Style = this.FindResource("MyStyle") as Style;
    

    The reason for it being that ElementStyle directly works in the column’s Content (ie. the TextBlock displaying a value)

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

Sidebar

Related Questions

I'd like to use a WPF ComboBox in a similar way to the Flag
How do you Clear/Remove DataBinding in Silverlight? similar to: Remove binding in WPF using
I am making an app in WPF in a style similar to Windows Explorer,
I am using Avalon in my WPF app. I want a window similar to
Is there anything similar to WPF and MVVM in Java world?
So in WPF there is a CommandParameter attribute that you can use in your
I want to create a WPF control similar to the example below. Check out
WPF seems to be lacking a RadioMenuItem class or similar functionality. In Windows.Forms ,
I see code similar to the following sprinkled about some native WPF controls: static
Not too long ago I started writing a docking library for WPF (similar to

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.