C# 在区域性更改后,如何强制WPF Toolkit DatePicker在UI中刷新?
我正在为使用.NET3.5的应用程序添加本地化。应用程序正在使用MVVM模式和命令来更改区域性。除了DatePicker控件在我单击它之前不会更改语言之外,一切都很正常。此时,所选日期文本将正确更改。控件中的下拉日历也不会更改语言,直到我将月份向前或向后移动一次 如何在运行命令以更改区域性后立即强制控件使用适当的语言刷新 我尝试了几件事都没有成功,包括:C# 在区域性更改后,如何强制WPF Toolkit DatePicker在UI中刷新?,c#,wpf,xaml,datepicker,wpftoolkit,C#,Wpf,Xaml,Datepicker,Wpftoolkit,我正在为使用.NET3.5的应用程序添加本地化。应用程序正在使用MVVM模式和命令来更改区域性。除了DatePicker控件在我单击它之前不会更改语言之外,一切都很正常。此时,所选日期文本将正确更改。控件中的下拉日历也不会更改语言,直到我将月份向前或向后移动一次 如何在运行命令以更改区域性后立即强制控件使用适当的语言刷新 我尝试了几件事都没有成功,包括: DatePickerControl.InvalidateVisual() DatePickerControl.UpdateLayout()
- DatePickerControl.InvalidateVisual()
- DatePickerControl.UpdateLayout()
- 在VM中SelectedDate的CultureChanged事件上触发NotifyPropertyChanged
- DatePickerControl.Dispatcher.Invoke(DispatcherPriority.Render,EmptyDelegate)
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>
应用文化
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));
}
}
公共类应用程序文化:INotifyCultureChanged
{
私有应用程序文化()
私有静态应用程序文化\u实例;
公共静态应用程序文化实例
{
得到
{
if(_instance==null)
_实例=新的应用程序文化();
返回_实例;
}
}
私有CultureInfo\u currentCulture=CultureInfo.InvariantCulture;
公共文化信息当前文化
{
获取{return\u currentCulture;}
设置
{
如果(!CultureInfo.Equals(value,_currentCulture))
{
_当前文化=价值;
NotifyCultureChanged(值);
}
}
}
公共事件事件处理程序已更改;
私有void NotifyCultureChanged(CultureInfo区域性)
{
如果(CultureChanged!=null)
CultureChanged(此,新CultureChangedEventArgs(文化));
}
}
在这种情况下,解决方案可能是更改用户交互模式。在分页应用程序中,我会切换到一个单独的页面以选择语言,并在更改时切换回原始页面。因此,页面将重新初始化,包括所有本地化的静态资源。在未分页的应用程序中,您可以在关闭并重新打开主窗口时使用对话框更改UI语言
在这两种情况下,诀窍都是在更改语言之前和之后保留ViewModel实例,以便在重新加载本地化资源时保留视图状态和输入的数据。虽然我没有完全遵循这一点,但它确实引导了我正确的方向。最后,我不得不从工作空间集合中删除视图模型,然后将它们重新插入到原始位置,以强制重新渲染控件。看起来有点老套,但很管用。
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));
}
}