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 6944921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:11+00:00 2026-05-27T13:22:11+00:00

I have a converter that takes a decimal value and converts it to a

  • 0

I have a converter that takes a decimal value and converts it to a brush (red for negative and black for positive input). I also created a style that I want to apply to all DataGridTextColumn that will take decimal values. If I inline the style for each DataGridTextColumn I can simply specify the related property on the Datacontext in the binding expression. However I don’t want to inline the style and simply have it as a resource, that way I can set the CellStyle to the resource. The problem is I don’t know what to put in the Binding of the value for the Foreground property. I want to be able to bind it to value that the cell to which it’s being applied is bound to.

Here is what I have:

    <Window.Resources>
        <!-- This converter takes a decimal value and returns a brush -->
        <conv:NumericValueBrushColorConverter x:Key="NumericValueBrushColorConverter"></conv:NumericValueBrushColorConverter>

        <Style x:Key="CurrencyStyle" TargetType="DataGridCell">
            <Setter Property="HorizontalAlignment"  Value="Right" />
            <Setter Property="Foreground"  Value="{Binding WhatGoesHere, Converter={StaticResource NumericValueBrushColorConverter}}"></Setter>
        </Style>
    </Window.Resources>             


    <DataGrid ItemsSource="{Binding CashReport}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}" CellStyle="{StaticResource CurrencyStyle}" />
            <DataGridTextColumn Header="Ending Cash Available" Binding="{Binding EndingBalance, StringFormat={}{0:C}}" CellStyle="{StaticResource CurrencyStyle}" />
        </DataGrid.Columns>
    </DataGrid>

Update #1

As per Jefim’s suggestion I should attempt to use the ElementStyle as it works directly on the rendered TextBlock as opposed to (CellStyle) the content control (whatever that is) that holds the TextBlock.

    <Style x:Key="CurrencyStyle" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment"  Value="Right" />
        <Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text, Converter={StaticResource NumericValueBrushColorConverter}}"/>
    </Style>

    <DataGrid ItemsSource="{Binding CashReport}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
            <DataGridTextColumn Header="Ending Cash Available" Binding="{Binding EndingBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
        </DataGrid.Columns>
    </DataGrid> 

This seems to work, however when I track my converter’s Convert method it seems to fire about 28 times or so with empty value being passed to the Convert method. After that all values flow as expected. When the grid renders everything looks right, there are no empty cells. What executes my converter the first 20+ times without values?

Update #2
I believe my current issue is unrelated to the original question so I moved it out to: IValueConverter executes more times than expected

  • 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-27T13:22:11+00:00Added an answer on May 27, 2026 at 1:22 pm

    You can (and probably should) use the ElementStyle / EditingElementStyle properties – they will allow you to style the TextBlock and TextBox that are within the cell. E.g.:

    <Style x:Key="CurrencyStyle" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment"  Value="Right" />
        <Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text, Converter={StaticResource NumericValueBrushColorConverter}}"/>
    </Style>
    
    <DataGrid ItemsSource="{Binding CashReport}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Beginning Cash Available" Binding="{Binding BeginningBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
            <DataGridTextColumn Header="Ending Cash Available" Binding="{Binding EndingBalance, StringFormat={}{0:C}}" ElementStyle="{StaticResource CurrencyStyle}" />
        </DataGrid.Columns>
    </DataGrid> 
    

    UPDATE

    Updated the code to the same as in the question’s update #1 so that people who look at the answer would see the correct version of code (by e36M3)

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

Sidebar

Related Questions

I have the following code that takes a double value and converts it to
I have written a converter that takes openstreetmap xml files and converts them to
I have a small C# function that takes any generic SQLDataReader and converts it
I have a function that takes longitude and latitude and converts it to x
I have this function called ProperCase that takes a string, then converts the first
I have and old(ish) C# method I wrote that takes a number and converts
I have to write a program that takes an Integer and converts it into
I have a converter that takes the text from a TextBlock that makes up
i have a javascript method that takes a date: convert(new Date(02/20/2010); how can i
I have a piece of code that the ARC converter turned into this... //

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.