简体   繁体   中英

Set culture with additional settings in WPF

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?

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:

十进制格式 WPF 绑定文化

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:

文化转换器绑定 WPF

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM