2013-02-08 73 views
0

我正在爲使用.NET 3.5的應用程序添加本地化。該應用程序使用MVVM模式和命令來更改文化。一切都運行良好,除非DatePicker控件在點擊它之前不會更改語言。此時,所選日期文本將會正確更改。直到我將該月向前或向後移動一次,控件中的下拉日曆也不會更改語言。如何強制WPF Toolkit DatePicker在文化更改後刷新UI?

只要命令運行以改變文化,我該如何強制控制刷新適當的語言?

我已經試過幾件事情沒有成功,包括:在VM

  • DatePickerControl

    • DatePickerControl.InvalidateVisual()
    • DatePickerControl.UpdateLayout()
    • 燒成NotifyPropertyChanged在CultureChanged事件SelectedDate .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> 
    

    視圖模型命令

    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)); 
        } 
    } 
    
  • 回答

    0

    在這種情況下,該解決方案可能是改變用戶交互模式。在分頁的應用程序中,我會切換到單獨的頁面以選擇語言,並在更改時切換回原始頁面。所以,頁面將被重新初始化,包括所有本地化但靜態的資源。在非分頁應用程序中,您可以使用對話框更改UI語言,同時關閉並重新打開主窗口。

    在這兩種情況下,關鍵是從之前和改變語言後保存視圖模型的實例,使視圖狀態和輸入的數據,而本地化的資源重新加載被保留。

    +0

    雖然我沒有按照這個完全是,它肯定使我在正確的方向。我最後不得不從我的工作區集合中刪除視圖模型,然後重新插入到原來的位置,以強制控制的重新渲染。似乎hacky,但它的作品。 – lanfeaer 2013-02-12 17:20:24