如何强制WPF Toolkit DatePicker在文化变革后在UI中刷新? [英] How to force WPF Toolkit DatePicker to refresh in UI after culture change?

查看:226
本文介绍了如何强制WPF Toolkit DatePicker在文化变革后在UI中刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为使用.NET 3.5的应用程序添加本地化。该应用程序正在使用MVVM模式和一个命令来改变文化。一切都很好,除了DatePicker控件不会更改语言,直到我点击它。此时,所选日期文本将正确更改。控制中的下拉式日历也不会更改语言,直到我向前或向后移动一个月一次。

I am working on adding localization to an application that uses .NET 3.5. The application is using MVVM pattern and a command to change the culture. Everything is working well except that the DatePicker control does not change language until after I click on it. At this point, the selected date text will change properly. The dropdown calendar in the control also will not change language until I move the month either forward or backwards once.

一旦运行命令来改变文化,我该如何强制使用正确的语言刷新控件?

How can I force the control to refresh with the proper language as soon as the command is run to change culture?

我已尝试过几件事情,包括:

I have tried several things with no success including:


  • DatePickerControl.InvalidateVisual()

  • DatePickerControl.UpdateLayout()

  • 在VM中为SelectedDate的CultureChanged事件触发NotifyPropertyChanged

  • DatePickerControl.Dispatcher.Invoke(DispatcherPriority.Render,EmptyDelegate)

  • DatePickerControl.InvalidateVisual()
  • DatePickerControl.UpdateLayout()
  • Firing NotifyPropertyChanged on the CultureChanged event for SelectedDate in VM
  • DatePickerControl.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate)

App.xaml.cs

protected override void OnStartup(StartupEventArgs e)
    {
        ApplicationCulture.Instance.CultureChanged += Instance_CultureChanged;
        base.OnStartup(e);
    }

    private void Instance_CultureChanged(object sender, CultureChangedEventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = e.Culture;
        System.Threading.Thread.CurrentThread.CurrentCulture = e.Culture;
    }

查看

<UserControl x:Class="ManageAppointmentsView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
<StackPanel>
    <TextBlock Margin="5" FontSize="15" Text="{Binding LocalizedResources.Resource.Date}" />

    <toolkit:DatePicker SelectedDate="{Binding SelectedDate}" SelectedDateFormat="Long" FontSize="15" VerticalContentAlignment="Center" 
                                DisplayDateStart="{Binding StartDate}" CalendarStyle="{StaticResource CalendarStyle}" x:Name="DatePickerControl" />
</StackPanel>
</UserControl>

ViewModel命令

ChangeLanguageCommand = new SimpleCommand
                                    {
                                        ExecuteDelegate = x =>
                                                              {
                                                                  var newCulture = x == null
                                                                                       ? "en-US"
                                                                                       : x.ToString();

                                                                  ApplicationCulture.Instance.CurrentCulture =
                                                                      new CultureInfo(newCulture);
                                                              }
                                    };

ApplicationCulture

public class ApplicationCulture : INotifyCultureChanged
{
    private ApplicationCulture() { }

    private static ApplicationCulture _instance;
    public static ApplicationCulture Instance
    {
        get
        {
            if (_instance == null)
                _instance = new ApplicationCulture();

            return _instance;
        }
    }

    private CultureInfo _currentCulture = CultureInfo.InvariantCulture;
    public CultureInfo CurrentCulture
    {
        get { return _currentCulture; }
        set
        {
            if (!CultureInfo.Equals(value, _currentCulture))
            {
                _currentCulture = value;
                NotifyCultureChanged(value);
            }
        }
    }

    public event EventHandler<CultureChangedEventArgs> CultureChanged;
    private void NotifyCultureChanged(CultureInfo culture)
    {
        if (CultureChanged != null)
            CultureChanged(this, new CultureChangedEventArgs(culture));
    }
}


推荐答案

在这种情况下,解决方案可能在于更改用户交互模式。在分页应用程序中,我将切换到单独的页面以选择语言,并在更改时切换回原始页面。所以,该页面将被重新初始化,包括所有本地化但是其他的静态资源。在非分页应用程序中,您可以使用对话框更改UI语言,同时关闭并重新打开主窗口。

In this case, the solution might be in changing the user interaction pattern. In a paged application, I'd switch to a separate page in order to select the language, and switch back to the original page when it is changed. So, the page would be initialized anew, including all localized but otherwise static resources. In a non-paged application, you could e.g. use a dialog to change the UI language, while you close and reopen the main window.

在这两种情况下,窍门是在更改语言之前和之后保留ViewModel实例,以便在本地化时保留视图状态和输入的数据资源重新加载。

In both cases, the trick would be to preserve the ViewModel instance from before and after changing the language, so that the view state and the entered data are preserved while the localized resources are reloaded.

这篇关于如何强制WPF Toolkit DatePicker在文化变革后在UI中刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆