views:

423

answers:

1

I have a multi-language silverlight application where the resources are stored in resx files for different languages and bound to the buttons' and labels' Content Properties inside the xaml.

When setting the thread's UI culture in the constructor of the silverlight's page every thing works fine, but when changing it based on user's selection (through combobox selection) the interface doesn't change. I need the page to redraw the controls and rebind to the resource files based on the new thread's UI culture.

A: 

I am not sure what mechanism you are using to bind your view to your localization repository, but I am guessing that the problem lies with notification.

The view will only update the data in a binding when it gets a notification event. Most likely, the object with the localization data that you are binding to is not sending notifications when the culture changes.

You might consider adding an INotifyPropertyChanged to the object that holds your localization strings. Then, add a method to that class called "NotifyThatEverythingChanged". In that method, just send that the property string.Empty has changed, which tells the UI to update everything in the data context.

In my case, I have the object that the RESX auto-generated for me called MyUIStrings. It has a bunch of static strings in it. I derive from that class, and add the functionality to notify that everything has changed. The UI will act accordingly:

public class NotifyableUIStrings : MyUIStrings, INotifyPropertyChanged
{
    public void NotifyThatEverythingChanged()
    {
        OnPropertyChanged(string.Empty);
    }

    protected void OnPropertyChanged(string propertyName)
    {
        var handlers = PropertyChanged;
        if(handlers != null)
            handlers(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
Brian Genisio
Can you please elaborate on how you use this class in the app. I've tried to do the same and the binding doesn't work, the content is empty in my bindings. If I register the "MyUIStrings" instead of the notify version, it works fine. This is for Silverlight 4.
SondreB
An easier way to implement NotifyThatEverythingChanged is to just do OnPropertyChanged(null). See the Remarks section here: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx
ArildF
@ArildF: Yeah. Thats right. Didn't know it at the time. String.Empty works too :)
Brian Genisio
@SondreB: I have done this in the past, outlined in this blog post: http://houseofbilz.com/archives/2009/03/15/binding-to-resources-in-silverlightwpf/
Brian Genisio
@Brian Genisio Thanks, that still though require you to type a bit extra ;-) I wanted to use the generated resource-class directly and avoid "String.SearchButton". I came up with this solution that I think works well: http://sondreb.com/blog/post/Simplifying-Text-Resources-in-Silverlight.aspx
SondreB