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

前端 未结 5 1458
醉话见心
醉话见心 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: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);
        }
    
    
    }
    

提交回复
热议问题