Search code examples
winui-3winui

How can I format a textbox number to curreny with 2 decimal places?


How can I format the text of a textblock as currency with two decimal places?

<TextBlock Text="{x:Bind ViewModel.Premium, Mode=TwoWay}"/>

I want to configure something like DisplayFormat="c2" and the result should be for instance 10,23$ Something that detects the configured global/thread currency.

I did not find a property under https://learn.microsoft.com/de-de/uwp/api/windows.ui.xaml.controls.textbox?view=winrt-22621

or a binding feature that allows that. Not sure if the numberbox would be appropriate. I need it only readable as info for the user.


Solution

  • You can use function binding, like this:

    <Window xmlns:sys="using:System" ...>
    ...
        <TextBlock Text="{x:Bind sys:String.Format('{0:C2}', ViewModel.Premium)}" />
    ...
    </Window>
    

    PS: can't be TwoWay in the case since it's now a computed value