简体   繁体   中英

WPF - How to change language property of a window dynamically

I have a window in application that is used to purchase products.Now there r two options Local or Foreign.If a user clicks on local the currency that is ,string format of my textbox holding Rate and Amount should have euros as currency and if user selects foreign it should be dollar.

Window_Purchase.Language = ?

Window_Purchase is the name of my window.

How can I change the language property at runtime.I don't want to change text language only the currency format .Thanks in advance.

If you have 2 or more resource files, for example:
(They need to be added under Properties in Solution Explorer )

Resources.resx 在此处输入图片说明

Resources.de.resx 在此处输入图片说明

They can be dynamically switched with by implementing INotifyPropertyChanged the following class.

namespace WpfApplication1.Properties
{
    using System.Globalization;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Properties;

    public class ResourceService : INotifyPropertyChanged
    {
        #region singleton members

        private static readonly ResourceService _current = new ResourceService();
        public static ResourceService Current
        {
            get { return _current; }
        }
        #endregion

        readonly Properties.Resources _resources = new Properties.Resources();

        public Properties.Resources Resources
        {
            get { return this._resources; }
        }

        #region INotifyPropertyChanged members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = this.PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        public void ChangeCulture(string name)
        {
            Resources.Culture = CultureInfo.GetCultureInfo(name);
            this.RaisePropertyChanged("Resources");
        }
    }
}

and the text (currency) you want to change has to bind this to receive PropertyChanged event like this:

<!-- Add xmlns:properties-->
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:properties="clr-namespace:WpfApplication1.Properties">

<TextBlock Text="{Binding Source={x:Static properties:ResourceService.Current}, Path=Resources.Currency, Mode=OneWay}"

Then, you can change the Culture ( Resources ) dynamically.
For example:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ResourceService.Current.ChangeCulture("de");
}

If i get right you wan't to change the culture info on application?

Application.CurrentCulture = System.Globalization.GetCultureInfo("en-us");

https://msdn.microsoft.com/en-us/library/system.windows.forms.application.currentculture(v=vs.110).aspx

Try this instend for the current form

System.Windows.FrameworkElement.LanguageProperty.OverrideMetadata(  
                typeof( System.Windows.FrameworkElement ),  
                new System.Windows.FrameworkPropertyMetadata(  
                    System.Windows.Markup.XmlLanguage.GetLanguage( System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag ) ) ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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