2017-08-17 149 views
-2

我有一个多语言的wpf项目。我正在使用ResourceDictionary来执行此操作。对于静态TextBlock我可以改变文本语言:WPF TextBlock绑定到多语言的ResourceDictionary

<TextBlock Text="{Binding Sample, Source={StaticResource Resources}}" /> 

我应该如何更改动态TextBlock文本。这似乎是不可能做这样:

<TextBlock Text="{Binding Sample} 

而且在后面的代码:

Sample = Resources.SampleText; 

如果这是不可能的。还有其他的选择吗?提前致谢!

+2

从这里开始阅读:[数据绑定概述](https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/data/data-binding-overview)。 – Clemens

回答

1

类,其中Sample属性定义应该实现INotifyPropertyChanged接口,提高变更通知:

public class Translations : INotifyPropertyChanged 
{ 
    private string _sample; 
    public string Sample 
    { 
     get { return _sample; } 
     set { _sample = value; OnPropertyChanged("Sample"); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

只有这样,你就可以通过简单的Sample源属性设置为一个新的动态更新TextBlock值为string