如何在WPF应用程序中全局设置文化?

时间:2009-08-12 12:08:51

标签: c# .net wpf xaml localization

我想根据用户偏好将WPF应用程序的文化设置为特定的文档。

我可以通过Thread.CurrentThread.Current(UI)Culture为当前线程执行此操作,但有没有办法为应用程序全局执行此操作(因此默认情况下会影响所有线程)?

5 个答案:

答案 0 :(得分:11)

没有办法为应用程序中的所有线程设置它,但是如果你在应用程序中创建线程,你可以自己设置文化,如上所述

要将Culture设置为Main Application,请使用以下代码段:

Dim newCulture As CultureInfo = new CultureInfo("fr-FR")
CurrentThread.CurrentCulture = newCulture

答案 1 :(得分:6)

尝试放

<UICulture>en-US</UICulture>

...在你的csproj文件中。

答案 2 :(得分:5)

好的,所以这就是我用来确保我的所有应用都是在美国文化中。

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
XmlLanaguage lang = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(lang));
FrameworkContentElement.LanguageProperty.OverrideMetadata(typeof(System.Windows.Documents.TextElement), new FrameworkPropertyMetadata(lang));

为了在文化中制作单个帖子,你可以制作

Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");

答案 3 :(得分:4)

或者你可以试试这个:

 FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))

答案 4 :(得分:0)

或者尝试构建适当的附加属性

public class CultureHelper : DependencyObject
{

    public string Culture
    {
        get { return (string)GetValue(CultureProperty); }
        set { SetValue(CultureProperty, value); }
    }




    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CultureProperty =
        DependencyProperty.RegisterAttached("Culture", typeof(string), typeof(CultureHelper), new FrameworkPropertyMetadata("en", CultureChanged));

    private static void CultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //For testing purposes in designer only 
        if (DesignerProperties.GetIsInDesignMode(d))
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)e.NewValue);
        }

    }

    public static void SetCulture(DependencyObject element, string value)
    {
        element.SetValue(CultureProperty, value);
    }

    public static string GetCulture(DependencyObject element)
    {
        return (string)element.GetValue(CultureProperty);
    }


}