WPF TextBlock在多语言中绑定到ResourceDictionary

时间:2017-08-17 05:45:48

标签: c# wpf multilingual resourcedictionary

我有一个多重的wpf项目。我正在使用ResourceDictionary来执行此操作。对于静态TextBlock,我可以通过以下方式更改文本语言:

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

但是我应该如何更改动态TextBlock文字。以这种方式做这件事似乎是不可能的:

<TextBlock Text="{Binding Sample}

在后面的代码中:

Sample = Resources.SampleText;

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

1 个答案:

答案 0 :(得分: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));
    }
}

只有这样,您才能动态更新TextBlock,只需将Sample来源属性设置为新的string值即可。