我在应用程序中有一个用于购买产品的窗口。现在有两个选项本地或外国。如果用户点击本地货币,我的文本框保持费率和金额的字符串格式应该有欧元作为货币如果用户选择外国,则应为美元。
Window_Purchase.Language =?
Window_Purchase是我的窗口名称。
如何在运行时更改语言属性。我不想仅使用货币格式更改文本语言。提前感谢。
答案 0 :(得分:1)
如果您有2个或更多资源文件,例如:
(需要在解决方案资源管理器中的Properties
下添加
可以通过实施以下类INotifyPropertyChanged
来动态切换它们。
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");
}
}
}
并且您想要更改的文本(货币)必须将其绑定到接收PropertyChanged
事件,如下所示:
<!-- 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}"
然后,您可以动态更改Culture
(Resources
)。
例如:
private void Button_Click(object sender, RoutedEventArgs e)
{
ResourceService.Current.ChangeCulture("de");
}
答案 1 :(得分:0)
如果我做对了,你不想改变申请时的文化信息吗?
Application.CurrentCulture = System.Globalization.GetCultureInfo("en-us");
答案 2 :(得分:0)
尝试这种方式来填写当前表格
System.Windows.FrameworkElement.LanguageProperty.OverrideMetadata(
typeof( System.Windows.FrameworkElement ),
new System.Windows.FrameworkPropertyMetadata(
System.Windows.Markup.XmlLanguage.GetLanguage( System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag ) ) );