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

huangapple go评论63阅读模式
英文:

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

问题

You can format the text of a TextBlock as currency with two decimal places by using the StringFormat property in your XAML code. Here's how you can modify your code:

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

This will format the value of ViewModel.Premium as currency with two decimal places, and it will automatically detect the configured global/thread currency.

There is no direct property for currency formatting in the TextBlock itself, but you can achieve the desired formatting using the StringFormat as shown above.

英文:

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

&lt;TextBlock Text=&quot;{x:Bind ViewModel.Premium, Mode=TwoWay}&quot;/&gt;

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.

答案1

得分: 0

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

英文:

You can use function binding, like this:

&lt;Window xmlns:sys=&quot;using:System&quot; ...&gt;
...
    &lt;TextBlock Text=&quot;{x:Bind sys:String.Format(&#39;{0:C2}&#39;, ViewModel.Premium)}&quot; /&gt;
...
&lt;/Window&gt;

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

huangapple
  • 本文由 发表于 2023年5月18日 13:31:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定