Welcome to the Xceed Community | Help
Community Search  
More Search Options

Making Negative Values Red

Sort Posts: Previous Next
  •  05-01-2012, 11:31 AM Post no. 32064

    Making Negative Values Red

    Hello.  This is yet another question about making the negative cells in my datagrid red.  I found a couple of posts concerning negative values, but the solutions don't quite fit my requirements.  I would like all of the cells in my grid which are negative to have a red foreground.  I would like this to happen regardless of the names of the fields because the names and numbers of the fields may change at runtime.  I would also like this to happen whether I set auto-generated-columns to true or false.

    I found this post http://xceed.com/CS/forums/post/17155.aspx that provides a solution that colors cells the way I'd like (although it has no code in it).  I would have to add fields to my datasource that look like IsFieldAPositive.  This is not an ideal solution for a few reasons.

    * First: This is a hack.  If I add a numeric column at some point in the future, I will need to remember to add a IsFieldXPositive field as well.  I will also need to edit my XAML code to make room for my new column whether or not my columns are autogenerated.

    * Second: This is not reusable code.  Every time I need to add a new grid in the future, I will need to add extra fields to its datasource's numeric fields and then edit the XAML code to use these new fields.  Ideally, when I add a new grid to my project, I should simply have to add a line or two of code to the XAML file.  One benefit of such a solution would be if I decide to change the color of the cells in the future, from Red to DarkRed, I would only need to edit just one line of code.

    * Third: This code does not work if I won't know the column names until runtime.  It also won't work if I don't know the number of columns that will be in my grid until runtime. 

    I also found this blog post http://xceed.com/CS/blogs/techside/archive/2011/07/06/datacell-styling-vs-cellcontenttemplate.aspx that does something similar to what I'd like.  It colors the values in two columns based on their relative values.  Higher values in a row are green and lower values in a row are red.  This solution is a little bit closer to what I'd like, but it still has problems.

    * Again I will run in to problems when I add numeric columns in the future.  I like that with this solution, I don't need to add extra columns to my datasource though.

    * Again the code is less than reusable.

    * Again this code will not work if I don't know the names of my numeric columns nor how many numeric columns I will have:

    I tried to tailor the second solution to my needs, but ran into an obstacle. My C# code is similar to the MultiValueColorConverter code in the blog post I linked.  My XAML code looks like this:

    <Style TargetType="{x:Type xcdg:DataCell}">

        <Setter Property="Foreground">

            <Setter.Value>

                <MultiBinding Converter="{StaticResource negativeRedType}">

                    <Binding RelativeSource="{RelativeSource Self}" Path="ParentColumn.FieldName"/>

                    <Binding Path="." />

                </MultiBinding>

            </Setter.Value>

        </Setter>

    </Style>

    The reason this code doesn't work is because that while it targets datacells, the binding path gives me an empty datacell.

    Is there some generic code I can use that looks at all datacells regardless of their fieldnames, and then checks that the values can be converted to decimals, and then sets the foreground color based on the value?

    P.S. The HTML controls in this "Write a New Post" page aren't working for me.  I'm using Chrome.

  •  05-07-2012, 9:37 AM Post no. 32082 in reply to 32064

    Re: Making Negative Values Red

    Hi Jordan,

    You can achieve this by using a simple converter that returns the correct brush color depending on the value it receives. You apply this converter directly on the value for the Foreground property on a DataCell by using a Style.

    For example:

       <local:NegativeConverter x:Key="myConverter" />
       <Style TargetType="{x:Type xcdg:DataCell}">
          <Setter Property="Foreground"
                  Value="{Binding RelativeSource={RelativeSource Self}, Path=Content, Converter={StaticResource myConverter}}">
          </Setter>
       </Style>


       public class NegativeConverter : IValueConverter
       {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
             Brush defaultColor = Brushes.Black;
             Brush negativeColor = Brushes.Red;

             string input;
             bool parse = false;
             bool negative = false;
             decimal numVal = 0;

             if (value != null)
             {
                input = value.ToString();

                try
                {
                   parse = Decimal.TryParse(input, out numVal);
                }
                catch (Exception ex)
                {
                   // do nothing (Converter will return default color)
                }
                finally
                {
                   if (parse && numVal < 0)
                   {
                      negative = true;
                   }
                }

                if (negative) { return negativeColor; }
             }
             return defaultColor;
          }

          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
             throw new NotImplementedException();
          }
       }

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
  •  05-07-2012, 11:02 AM Post no. 32083 in reply to 32082

    Re: Making Negative Values Red

    Thank you, this works great.  This is exactly what I needed.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.