Если у вас есть 2 или более файлов ресурсов, например:
(Они должны быть добавлены при Properties
в Solution Explorer)
Resources.resx
Resources.de.resx
Они могут быть динамически переключаться с помощью реализации 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");
}
Нет, я просто хочу его для конкретного окна не требуется для всего приложения – user5552042
я попробовал ваш данный код .the класс приложение не имеет CurrentCulture свойства – user5552042