简体   繁体   中英

Getting digits after decimal point

I'm binding an SqlMoney type to a TextBox in WPF and I'm trying to put digits after decimal point into superscript. This is what I've got so far:

<TextBlock>
    <Run FontSize="50">1000</Run>
    <Run BaselineAlignment="TextTop" TextDecorations="Underline" FontSize="26">00</Run>
</TextBlock>

Is there a simple way to this in WPF using something like StringFormat or I need to split it somewhere else in code and then bind?

Edit: Ok, I might have explained it badly. This is actual implementation in code right now:

Model property
public SqlMoney Price { get; }

View
<TextBlock Text="{Binding Mode=OneWay, Path=Price}" />

Effect: Effect - before and after

Is there way to make View handle splitting or I need to change implementation in Model or VM?

When you use binding, you can use StringFormat to do what you need. In your example there is no binding so no place for StringFormat .

Here is a StringFormat you can use when binding:

StringFormat={}{0:00.00}

Example of binding with StringFormat (assuming Superscript is a property of you DataContext ):

<TextBlock>
    <Run FontSize="50">1000</Run>
    <Run BaselineAlignment="TextTop" TextDecorations="Underline" FontSize="26" Text="{Binding Superscript, StringFormat={}{0:00.00}}"/>
</TextBlock>

If you don't have yet the DataContext set up and you want a self contained example you could create a Resource and bind to it:

<TextBlock>
    <TextBlock.Resources>
        <sys:Double x:Key="Superscript">0</sys:Double>
    </TextBlock.Resources>
    <Run FontSize="50">1000</Run>
    <Run BaselineAlignment="TextTop" TextDecorations="Underline" FontSize="26" Text="{Binding Source={StaticResource Superscript}, StringFormat={}{0:00.00}, Mode=OneWay}"/>
</TextBlock>

In this example I had to use OneWay binding because I'm binding to a static resource.

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM