1

I'm trying to pass my current culture (that has a custom decimal symbol) to WPF, so that it will display bound values according to my region and language settings in windows.

My researches always ended up with the a solution similar to this, which passes the language tag, but not any additional settings (like the decimal symbol).

How can I force WPF to use the whole current culture and not only the default language settings?

Questions about possible a possible workaround:

Can I somehow pass the current culture to the default value converters used by WPF? Or maybe override them?

Community
  • 1
  • 1
AntiStoopMode
  • 231
  • 2
  • 12

2 Answers2

2

There's couple options. Maybe the easiest one is to wrap the values you want to databind to screen and call ToString for them. For example, if you have:

    public decimal Value
    {
        get { return this.value; }
        set
        {
            if (value == this.value) return;
            this.value = value;
            OnPropertyChanged();
        }
    }

Wrap it inside your ViewModel like this:

    public decimal Value
    {
        get { return this.value; }
        set
        {
            if (value == this.value) return;
            this.value = value;
            OnPropertyChanged("ValueString");
        }
    }

    public string ValueString
    {
        get { return this.value.ToString(CultureInfo.CurrentCulture); }
    }

And bind your UI against this new property:

        <TextBlock x:Name="Result" Text="{Binding ValueString}" Grid.Row="0"/>

This way you will automatically get the formatting based on your computer's culture settings:

Decimal formatting WPF binding culture

Another alternative is to use the method presented in here: https://stackoverflow.com/a/19796279/66988

So you need a custom Binding class:

public class CultureAwareBinding : Binding
{
    public CultureAwareBinding(string path)
        : base(path)
    {
        ConverterCulture = CultureInfo.CurrentCulture;
    }
}

And then you have to use that in your XAML:

        <TextBlock x:Name="Result" Text="{wpfApplication9:CultureAwareBinding Value}" Grid.Row="0"/>

After which you should see the desired output:

Culture converter binding WPF

Community
  • 1
  • 1
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
  • We (the team) decided to not support any changed format settings, because it would take too much effort to change all the bindings or properties. But I created [a new suggestion on vs uservoice](https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7144712-support-additional-region-and-language-settings) , to support customized formats. – AntiStoopMode Feb 26 '15 at 10:43
0
using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Markup;

namespace WPF_CultureExample
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("tr-TR");
            var currentCulture = Thread.CurrentThread.CurrentCulture.Name;
            var ci = new CultureInfo(currentCulture)
            {
                NumberFormat = { NumberDecimalSeparator = "," },
                DateTimeFormat = { DateSeparator = "." }
            };
            Thread.CurrentThread.CurrentCulture = ci;
            Thread.CurrentThread.CurrentUICulture = ci;

            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

            base.OnStartup(e);
        }
        
    }
}

You can recode the OnStartup() method in the backend codes of the App.xaml file.