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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:39:40+00:00 2026-05-28T13:39:40+00:00

I am trying to apply some conditional formatting on a WPF DataGrid.The requirement is

  • 0

I am trying to apply some conditional formatting on a WPF DataGrid.The requirement is as follows. For any cell on the grid, if the content is an integer, the display format should be zero decimal places. If the contents have decimal places, then display them to the default value defined in the stringformat.

Does anybody have any idea as to how I could achieve this kind of conditional formatting? I have written a converter which can check for the existence of decimals but I have been unable to work out how I would be able to apply this in my XAML to either my cell style or my text column.

Cell Style – This is the style that I would like to define

<Style x:Key="MyCellStyle" TargetType="DataGridCell">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>

Grid Column – I would to apply the style applied to this column

<DataGridTextColumn x:Key="ColumnName"
                    Header="ColumnName"
                    SortMemberPath="MyColumnSort"
                    Binding="{Binding myColBinding, StringFormat={}{0:N2}}"
                    IsReadOnly="True"
                    Width="40" />

Converter – I would use this converter to determine whether this style gets applied.

public class NoDecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int converted;
            int.TryParse(value.ToString(), out converted);
            value.ToString();
            return int.TryParse(value.ToString(), out converted);
        }

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

I tried to do it by adding a datatrigger to my cell style as below but I got an error stating “Cannot find the Style Property ‘StringFormat’ on the type ‘System.Windows.Controls.DataGridCell’.”

    <Style x:Key="MyCellStyle" TargetType="DataGridCell">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Style.Triggers>             
            <DataTrigger Binding="{Binding NoDecimalConvertor}" Value="False" >                 
                <Setter Property="StringFormat" Value="true" />             
            </DataTrigger>         
        </Style.Triggers>     
</Style>

I have been banging my head against this for days so any guidance would be appreciated as to the best way to approach this.

  • 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-28T13:39:41+00:00Added an answer on May 28, 2026 at 1:39 pm

    Your converter is currently returning a boolean to indicate whether or not it could parse an integer from the string value. Instead, you should try something like this:

    public class NoDecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int converted;
            if (int.TryParse(value.ToString(), out converted))
                 return converted.ToString();
    
            double convertedDouble;
            if (double.TryParse(value.ToString(), out convertedDouble))
                 return convertedDouble.ToString();
    
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    This will first try to parse an int, and return it (as a string) if successful. If not, it will try a double and return it – here you could specify any string format to get the desired number of decimal places etc. Finally, if it can’t parse either it will return the original value.

    You can then set that on your column using:

    <DataGridTextColumn x:Key="ColumnName"
                    Header="ColumnName"
                    SortMemberPath="MyColumnSort"
                    Binding="{Binding myColBinding, Converter={StaticResource MyConverter}}"
                    IsReadOnly="True"
                    Width="40" />
    

    Note: you will need to instantiate your converter somewhere in the resources with a key of MyConverter.

    If you want to be able to specify a default number of decimal places, you could pass it in as a converter parameter.

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

Sidebar

Related Questions

I am trying to apply conditional formatting of certain table cells in my ReportViewer
I need to apply some text formatting when displaying items in a WPF ListView.
I am trying to apply some transformations on images using a CGContextRef. I am
I am trying to apply some CSS class on the options of a drop
I'm starting to read about design patterns and trying to apply them to some
I'm trying to extend the JavaScript on a page I maintain to apply some
I'm relatively new to TDD, and still trying to learn to apply some of
I'm trying to apply some CSS to the last .topic-wrapper div (which is inside
I've been programming for many years, recently i've been trying to apply some of
I have a given markup that I'm trying to apply to some style on.

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.