开发者

Force UI update on the fly, after changing current culture in WPF

I need to support UI language change through the application menu. The texts are localized using resource files (similar to approach 1 here)

if I set the Thread.CurrentThread.CurrentUICulture before the ctor calls to InitializeComponent(), the UI is changed as it s开发者_如何转开发hould.

However, if the CurrentUICulture is changed during the normal run of the application, the controls are not updated (i.e. the text remains the same, regardless of the current culture).

Is there a way to force the controls to refresh according the the CurrentUICulture?


You could use the ResourceDictionary approach along with the DynamicResourceMarkupExtension. Using this approach you can swap out the resource dictionary representing the language the user has selected and the DynamicResourceMarkupExtension will ensure the new value is reflected in the UI.

Also, if you're willing to venture into the realm of a custom solution, there is the LocalizeMarkupExtension which is provided in this WPF tutorial.


Assuming the DataContext for the menu is an object that implements INotifyPropertyChanged, you can refresh all controls by specifying null (nothing) in the PropertyChanged event...

from msdn:

The PropertyChanged event can indicate all properties on the object have changed by using either a null reference (Nothing in Visual Basic) or String.Empty as the property name in the PropertyChangedEventArgs.


A way that I tried and works well is this method, to call after the assignment of the property in the ViewModel:

private void AllowUiToUpdate()
{
    var frame = new DispatcherFrame();

    var dispatcherOperationCallback = new DispatcherOperationCallback(delegate
        {
            frame.Continue = false;
            return null;
        });

    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, dispatcherOperationCallback, null);

    Dispatcher.PushFrame(frame);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜