Search code examples
silverlightlocalizationculture

Silverlight ignoring CurrencyGroupSeparator


Thread.CurrentThread.CurrentCulture = New CultureInfo("sv-SE")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("sv-SE")

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator = " "
Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyGroupSeparator = " "

Im trying to do:

   <TextBlock Text={Binding decimalValue, StringFormat=c2}/>

It sets the culture properly and adds the "kr" which is the swedish currency symbol. However, doesnt honor the group separator setting. Even if I set it to "-" or whatever it doesnt work.

Big questionmark? Bug?


Solution

  • I am not sure if you were able to work around this problem, but since nobody answered, I will.
    For starters, I am not sure if your backing code runs in the same thread as presentation layer; probably not, that is I believe Silverlight creates its own visual thread. In other words setting thread-wide CultureInfo will not resolve your problem.

    There are (at least) two ways to resolve this issue:
    1. Play with StringFormat attribute to set custom format.
    2. Create dynamic property in the backing code which will format the value for you. Please find this imperfect example:

    public decimal Quote { get; set; }
    
    // Formats value of Quote property
    public string FormattedQuote
    {
        get
        {
            CultureInfo swedishCulture = new CultureInfo("sv-SE");
            swedishCulture.NumberFormat.CurrencyGroupSeparator = " ";
            return Quote.ToString("c2", swedishCulture);
        }
    }
    

    And in your XAML code, you do not need specify format, so you would only do this:

    <TextBlock Name="textBlock1" Text="{Binding FormattedQuote}" DataContext="{Binding ElementName=textBlock1}" />