Reputation: 33
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.
Upvotes: 2
Views: 2867
Reputation: 1421
If you have 2 or more resource files, for example:
(They need to be added under Properties
in Solution Explorer)
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");
}
Upvotes: 1
Reputation: 21
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 ) ) );
Upvotes: 0
Reputation: 21
If i get right you wan't to change the culture info on application?
Application.CurrentCulture = System.Globalization.GetCultureInfo("en-us");
Upvotes: 0