簡體   English   中英

文化更改后,如何強制WPF Toolkit DatePicker在UI中刷新?

[英]How to force WPF Toolkit DatePicker to refresh in UI after culture change?

我正在為使用.NET 3.5的應用程序添加本地化。 該應用程序正在使用MVVM模式和用於更改區域性的命令。 一切工作正常,除了DatePicker控件在單擊它之前不會更改語言。 此時,所選日期文本將正確更改。 在我將月份向前或向后移動一次之前,控件中的下拉日歷也不會更改語言。

一旦運行命令以更改區域性,如何強制控件以正確的語言刷新?

我嘗試了幾項沒有成功的事情,包括:

  • DatePickerControl.InvalidateVisual()
  • DatePickerControl.UpdateLayout()
  • 對VM中的SelectedDate的CultureChanged事件觸發NotifyPropertyChanged
  • 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);
                                                              }
                                    };

應用文化

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語言。

在這兩種情況下,技巧都是在更改語言之前和之后保留ViewModel實例,以便在重新加載本地化資源時保留視圖狀態和輸入的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM