简体   繁体   English

在 WPF 中使用其他设置设置区域性

[英]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.我正在尝试将我当前的文化(具有自定义十进制符号)传递给 WPF,以便它会根据我在 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).我的研究总是以类似于this的解决方案结束,它传递语言标签,但没有任何其他设置(如十进制符号)。

How can I force WPF to use the whole current culture and not only the default language settings?如何强制 WPF 使用整个当前文化而不仅仅是默认语言设置?

Questions about possible a possible workaround:关于可能的解决方法的问题:

Can I somehow pass the current culture to the default value converters used by WPF?我可以以某种方式将当前文化传递给 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.也许最简单的方法是将要数据绑定的值包装到 screen 并为它们调用 ToString 。 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:将它包裹在您的 ViewModel 中,如下所示:

    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:并将您的 UI 绑定到这个新属性:

        <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另一种选择是使用此处介绍的方法: https : //stackoverflow.com/a/19796279/66988

So you need a custom Binding class:所以你需要一个自定义的 Binding 类:

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

And then you have to use that in your XAML:然后你必须在你的 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.您可以在 App.xaml 文件的后端代码中重新编码 OnStartup() 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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