0
我正在爲使用.NET 3.5的應用程序添加本地化。該應用程序使用MVVM模式和命令來更改文化。一切都運行良好,除非DatePicker控件在點擊它之前不會更改語言。此時,所選日期文本將會正確更改。直到我將該月向前或向後移動一次,控件中的下拉日曆也不會更改語言。如何強制WPF Toolkit DatePicker在文化更改後刷新UI?
只要命令運行以改變文化,我該如何強制控制刷新適當的語言?
我已經試過幾件事情沒有成功,包括:在VM
- 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));
}
}
雖然我沒有按照這個完全是,它肯定使我在正確的方向。我最後不得不從我的工作區集合中刪除視圖模型,然後重新插入到原來的位置,以強制控制的重新渲染。似乎hacky,但它的作品。 – lanfeaer 2013-02-12 17:20:24