2015-11-11 72 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