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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:03:12+00:00 2026-05-27T09:03:12+00:00

I have a converter that takes the text from a TextBlock that makes up

  • 0

I have a converter that takes the text from a TextBlock that makes up a DataGridCell and converts it to a red or black brush depending if the value is negative or positive. However the converter executes more times than there are items in the datasource of the grid. For example if I simply bind a collection that contains 1 object, the converter executes 2 times. The first time the value parameter is an empty string, the second time it actually contains the value that I would expect. If I add more objects to the list, the number of initial “empty” executions grows. What am I doing wrong?

<Window.Resources>
    <conv:NumericValueBrushColorConverter x:Key="NumericValueBrushColorConverter"></conv:NumericValueBrushColorConverter>
</Window.Resources>

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


[ValueConversion(typeof(string), typeof(SolidColorBrush))]
internal class NumericValueBrushColorConverter : IValueConverter
{
    static int i = 0;

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string strValue = value as string;
        i++;
        if(string.IsNullOrEmpty(strValue)) return new SolidColorBrush(Colors.Black);

        if (strValue.StartsWith("("))
        {
            return new SolidColorBrush(Colors.Red);
        }
        else
        {
            return new SolidColorBrush(Colors.Black);
        }
    }
    ...
}   
  • 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-27T09:03:13+00:00Added an answer on May 27, 2026 at 9:03 am

    Well, first the TextBlock in the grid row is created and thus empty (= first execution) and then the content is bound to the value of the current item (= second execution). Nothing wrong here.

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

Sidebar

Related Questions

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 a function that takes 2 returned variables from another function to write
I have an UITextView which takes data from XML. Now that XML node is
I have written two methods that take and return a string. One converts from
I have a grid where I want to take the text from that cell,
I have an ANTLR3 simple grammar parser that takes short lines of text and
i have a javascript method that takes a date: convert(new Date(02/20/2010); how can i

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.