Domanda

I have the following code in XAML:

<TextBlock Text="{Binding XValue}" />

The XValue contains a lot of decimals(1952.230822830529)

I want it to contain only 2 decimals like 1952.23 and i want to do this in the XAML code.

I have searched the internet but not found a solution to my problem - so is it even to do this in XAML using string format?

È stato utile?

Soluzione

This is a duplicate of this post (also the top result on google): How to format number of decimal places in wpf using style/template?

Try one of these:

<TextBox Text="{Binding XValue, StringFormat=N2}" />
<TextBox Text="{Binding XValue, StringFormat=#,#.00}" />

Altri suggerimenti

If XValue is number (not string type) you could use

<TextBlock Text="{Binding XValue, StringFormat={}{0:N2}}"/>

Create a XAML Converter and call Math.Round() in its Convert() method. See MSDN

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top