2015-11-11 70 views
1

我在应用程序中有一个窗口,用于购买products.Now有两个选项Local或Foreign.If用户点击本地货币即字符串格式为我持有利率和金额的文本框应该有欧元作为货币,如果用户选择外国,它应该是美元。WPF - 如何动态更改窗口的语言属性

Window_Purchase.Language =?

Window_Purchase是我的窗口的名称。

如何在运行时更改语言属性。我不希望仅更改文本语言的货币格式。提前感谢。

回答

0

如果你有2个或更多的资源文件,例如试试这个instend当前形式

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


(他们需要在Properties下添加解决方案资源管理器

Resources.resx enter image description here

Resources.de.resx enter image description here

它们可以动态地实现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}" 

然后,您可以在CultureResources)动态变化。
例如:

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

怎么ü在字符串添加€一个特定的窗口资源文件的价值? – user5552042

+0

@ user5552042只是复制并粘贴它。 – jhmt