问题描述
在一个小的Wpf应用程序中,使用DatePicker绑定到DateTime属性。
当用户的区域和语言设置,数字和日期格式为德语时,日期显示为德语,日历显示德语月份名称。
现在我想用美式英语在MainWindow的c'tor中,我在InitializeComponent()之前添加了(在InitializeComponent())之后的情况下相同的情况):
In a small Wpf application, use a DatePicker bound to a DateTime property.When the user's region and language settings, and number and date format are German, the date is displayed in German, and the calendar shows German month names.Now I wanted to get it in US-English. In the c'tor of MainWindow I added before InitializeComponent() (same situation when doing that after InitializeComponent()):
string uiLanguage = ConfigurationManager.AppSettings["UILanguage"]; //"en-US"
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(uiLanguage);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(uiLanguage)));
虽然这与文本框一起使用,但它对DatePicker没有影响。
然后我是骗子,并创建了一个新用户约翰英语,以约翰英语的身份登录,将他的显示语言设置为英文,日期和数字格式为美英。现在,DatePicker始终以美英格式显示日期,日历显示英文月份名称,即使我将程序的语言设置为德语。
怎么可以解决?可以解决吗?
While that works with textboxes, it has no effect with the DatePicker.Then I was cheaky and created a new user "John English", logged in as "John English", set his display language to English and the date and number format to US-English. Now the DatePicker always displays the date in the US-English format and the calendar shows English month names, even when I set the language of my program to German.How can that be resolved? Can it be resolved at all?
推荐答案
在App.xaml的代码中添加以下代码:
In the codebehind of the App.xaml add the following code:
public App()
{
EventManager.RegisterClassHandler(typeof(DatePicker), DatePicker.LoadedEvent, new RoutedEventHandler(DatePicker_Loaded));
}
void DatePicker_Loaded(object sender, RoutedEventArgs e)
{
var dp = sender as DatePicker;
if (dp == null) return;
var tb = GetChildOfType<DatePickerTextBox>(dp);
if (tb == null) return;
var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
if (wm == null) return;
wm.Content = "[Put your text here]";
}
[Ontopic;]]
尝试设置CurrentCulture和CurrentUICulture。
[Ontopic ;)]Try setting both CurrentCulture and CurrentUICulture.
//Set default culture to Nl
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
ref for GetChildOfType
这篇关于如何本地化DatePicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!