How can i globally set the Culture in a WPF Application?

前端 未结 5 1452
醉话见心
醉话见心 2020-12-10 13:01

I would like to set the Culture of a WPF application to a specific one based on user preferences.

I can do this for the current thread via Thread.CurrentThrea

相关标签:
5条回答
  • 2020-12-10 13:42

    there is no way to set it for all threads in the application, however if you are creating the threads in your application you can set the culture for everyone by yourself as you mentioned above

    To set the Culture to the Main Application use the following snippet code:

    Dim newCulture As CultureInfo = new CultureInfo("fr-FR")
    CurrentThread.CurrentCulture = newCulture
    
    0 讨论(0)
  • 2020-12-10 13:42

    ok so this is what I use in order to make sure all of my app is in a en-US culture.

    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));
    

    in order to make a single thread in a culture you can make

    Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");
    
    0 讨论(0)
  • 2020-12-10 13:50

    Try putting

    <UICulture>en-US</UICulture>
    

    ... in your csproj file.

    0 讨论(0)
  • 2020-12-10 13:54

    Or try building an appropriate Attached Property like this

    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);
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-10 14:03

    Or you could try this:

     FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))
    
    0 讨论(0)
提交回复
热议问题