BarryLib BarryLib - 1 year ago 61
C# Question

WPF TextBlock binding to ResourceDictionary in mutilingual

I have a mutilingual wpf project. I am using ResourceDictionary to do this. For static

TextBlock
I can change the Text language by:

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


BUT how should I change a dynamic
TextBlock
Text. It seems impossible to do it in this way:

<TextBlock Text="{Binding Sample}


And in the code behind:

Sample = Resources.SampleText;


If this is impossible. Is there any other options? Thanks in advance!

mm8 mm8
Answer Source

The class where the Sample property is define should implement the INotifyPropertyChanged interface and raise change notifications:

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));
    }
}

Only then you will be able to update the TextBlock dynamically by simply setting the Sample source property to a new string value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download