How to set and change the culture in WPF

后端 未结 7 1636
孤独总比滥情好
孤独总比滥情好 2020-11-30 00:02

I have a .NET 4.0 WPF application where the user can change the language (culture) I simply let the user select a language, create a corresponding CultureInfo and set:

7条回答
  •  温柔的废话
    2020-11-30 00:15

    I never found a way to do exactly what I asked for in the question. In my case I ended up solving it by having all my usercontrols inherit from a superclass that contained this:

    /// 
    ///   Contains shared logic for all XAML-based Views in the application. 
    ///   Views that extend this type will have localization built-in.
    /// 
    public abstract class ViewUserControl : UserControl
    {
        /// 
        ///   Initializes a new instance of the ViewUserControl class.
        /// 
        protected ViewUserControl()
        {
            // This is very important! We make sure that all views that inherit 
            // from this type will have localization built-in. 
            // Notice that the following line must run before InitializeComponent() on 
            // the view. Since the supertype's constructor is executed before the type's 
            // own constructor (which call InitializeComponent()) this is as it 
            // should be for classes extending this
            this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
        }
    }
    

    When the user changes the language I then create new instances of any usercontrols that are currently running.

    This solved my problem. However, I would still like a way to do this "automatically" (i.e. without having to keep track of any instantiated objects).

提交回复
热议问题