1

I have this number 1234.456 it has 3 decimal places, this can be very confuing for my users some of them interpret the nunber as a thousands but still they need it.

How can i do to style those numbers?

Someting like this image:

enter image description here

NOTE: Maybe show decimal places in other color.

Any help would be very apreciated.

EDIT 1: All I want is an Style (xaml) template to apply, the edition mode must show the number normal to allow the user to modify it. For now I'm totally lost I'm a beginer with it.

Note: I use MVVM as a primary architecture, Binding required for my XAML template

Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101
  • 1
    [Decimal point](http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimalseparator.aspx) is what most users are expecting to see. But this symbol is culture-dependent (some use `,`, others `.`). Output decimal value with user culture settings taken into account (use proper overload of [ToString](http://msdn.microsoft.com/en-us/library/system.decimal.tostring.aspx)), this way user will not get *confused*. – Sinatr Jan 12 '15 at 13:55
  • @Sinatr How would you superscript with an overload? – paparazzo Jan 12 '15 at 14:05
  • @Blam, do not understand the question. Do you mean how to use converters to display user locale friendly value or how to create xaml element extension? – Sinatr Jan 12 '15 at 14:09
  • @Sinatr User asked for a superscript and you said use proper overload. – paparazzo Jan 12 '15 at 14:13
  • @Blam, I did not provide an answer, but a comment in the style *do not do that* :) (didn't knew translation of word *superscript* and understood your comment wrongly, sorry!). – Sinatr Jan 12 '15 at 15:58
  • have a look at this [link](http://stackoverflow.com/questions/2095583/set-superscript-and-subscript-in-formatted-text-in-wpf) – XAMlMAX Jan 12 '15 at 17:47

1 Answers1

2

you'd have to put two elements on the page, one for the integer part and one for the decimal part. Then style them as you would expect. Something like this:

<TextBlock x:Name="IntPart" Text="1234." FontSize="12" />
<TextBlock x:Name="DecPart" Text="456" Margin="0,0,0,3" FontSize="8"  />

With Bindings:

<TextBlock Text="{Binding IntPart}" FontSize="12" />
<TextBlock Text="{Binding DecPart}" Margin="0,0,0,3" FontSize="8"  />

With Bindings and converters

<my:IntPartConverter x:Key="MyIntPartConverter" />
<my:DecPartConverter x:Key="MyDecPartConverter" />

<TextBlock Text="{Binding MyNumber, Converter={StaticResource MyIntPartConverter}}" FontSize="12" />
<TextBlock Text="{Binding MyNumber, Converter={StaticResource MyDecPartConverter}}" Margin="0,0,0,3" FontSize="8"  />

C#

public class IntPartConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value;
    }

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

public class DecPartConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (double)value - (int)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • 2
    If you want to use binding you could us a converters to pull out left and right of the decimal. – paparazzo Jan 12 '15 at 14:07
  • indeed. i chose to only show static data for this example since the question is more about styling from what i could tell. – DLeh Jan 12 '15 at 14:08
  • Tks for your help, I'm looking for a ``Style`` to apply, and binding is required. – Juan Pablo Gomez Jan 13 '15 at 13:36
  • I have updated my answer to show how you could do it as Blam described with converters and a single bound field of `MyNumber` – DLeh Jan 13 '15 at 13:38
  • Very tks, I'm going to put all this into custom control. – Juan Pablo Gomez Jan 13 '15 at 13:45
  • that is a good idea, if you plan on using this in several places. please mark the this as the answer if it answered your question. – DLeh Jan 13 '15 at 13:46
  • @DLeh using **(int)value** cast may cause exceptions. Use **Math.Truncate((double)value)** do the job almost all numeric values for the converter – Juan Pablo Gomez Jan 14 '15 at 14:34
  • feel free to edit my answer to fix that kind of bug. I didn't really do my research on the best way to get the intpart and decpart of a number. – DLeh Jan 14 '15 at 14:38