Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a TextBlock that is binding to a string property which is a demical number. On my view i want to format it to one digit after decimal point.
I tried a few approaches but none worked. Any idea could be wrong or what else to try?

What I have tried:

My TextBlock xaml:
<TextBlock Grid.Row="5" Text="{Binding SelChem.Lel, StringFormat={}{0:F1}}"/>

I also tried:
<TextBlock Grid.Row="5" Text="{Binding SelChem.Lel, StringFormat=N2}"/>
Posted
Updated 26-Jan-20 23:25pm
v2
Comments
Richard Deeming 28-Apr-17 11:23am    
StringFormat=N2 works fine for me.

Check the Visual Studio output window for data binding errors.
Member 13063329 28-Apr-17 11:26am    
Tried that too. Data is being displayed correctly just not formatted so no binding errors.
Richard Deeming 28-Apr-17 11:28am    
What's the property type?
Member 13063329 28-Apr-17 11:29am    
In SelChem.lel SelChem is my model object and lel is string property
Richard Deeming 28-Apr-17 11:32am    
Well, there's the problem then! :)

The StringFormat only applies to types which are IFormattable - eg: double, float, DateTime, etc.

A string is not IFormattable, so the StringFormat is ignored.

Either expose the number as a numeric type, or change the format you use when you convert the number to a string.

1 solution

You can write a converter to be referenced in the binding. For instance:

C#
public class RoundMyNumberConverter : IValueConverter
{
    public object ConvertNumber(object value, Type targetType, object paramater, CultureInfo culture)
        {
            double result = 0;
            if (value != null && (double.TryParse(value.toString(), out result)))
                return Math.Round(result,1);
            else
                return value;
        }

    public object ConvertBack(object value, Type targetType, object paramater, CultureInfo culture)
        {
            return value;
        }
}


Note: The Math.Round(result, 1<- this is how many places you want after the decimal) so the converter will take in the binding value, run it through the converter and then display the value created.

Then you just reference this inside the xaml as a converter.

Note: Remember to reference your project and assembly if it is outside your current project like:
HTML
xmlns:cnvrtr="clr-namespace:MyConverterProject;assembly=MyConverterProject


Also you need to list it as a resource inside your UserControl or Grid?
HTML
<Grid>
    <Grid.Resources>
        <cnvrtr:RoundMyNumberConverter x:key="myConverter"/>
    </Grid.Resources>

    <TextBlock Grid.Row="5" Text="{Binding SelChem.Lel, Converter={StaticResource myConverter}}"/>
</Grid>


Let me know if you have anymore questions!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900