Question

I've got a double, for example

double d = 4.323d;

And I want to display it in a TextBlock on a Silverlight 4 application, but the display should be this:

4.32

And I cannot change the StringFormat on the binding whatsoever.

The exception is that if the number is this:

double d2 = 4d;

Then it should display

4, not 4.00.

And the worst exception is that it should take in account the current UI culture, which implies that if the app is deployed in the US it should use a . as a decimal seperator, and in Europe it should use a , (well not in the UK, but you get the point...)

I could set the defaultformat to #.## IF I were able to change the StringFormat, but I want to do it through CultureInfo

Was it helpful?

Solution

I'm going to assume for the moment that you believe you can't use StringFormat in binding because it doesn't use CultureInfo. That being the case add these two usings to your user control code behind :-

using System.Windows.Markup;
using System.Threading;

and then add this to its constructor:-

Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

Now a binding with StringFormat=#.## will use the appropriate decimal separator for the current culture.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top