4

是否可以仅为 WPF 中的一个窗口更改系统语言?

我知道,InputLanguageManager但我认为它会改变整个系统的语言。

4

2 回答 2

17

InputLanguageManager does exactly what you are asking for. It changes the keyboard layout for the current application.

The keyboard layout is kept by the OS for each running application. Eg. if you open Notepad and switch to Russian, the open IE and switch to English, when you activate the Notepad application, your keyboard locale will still be Russian.

The following line changes the keyboard locale just for the current application:

InputLanguageManager.Current.CurrentInputLanguage = new CultureInfo("el-GR");

The system language (or rather, the system locale) and the keyboard layout are completely different concepts. The keyboard layout is the layout of your keyboard.

There are three different locales used in a .NET application:

  • The UI locale is the locale used to display messages and select localized UI strings and layouts. You can change a thread's UI locale by setting its Thread.CurrentUICulture property. Its initial value is governed by the OS's display language in regional settings
  • The thread's locale is used to parse strings and convert dates and numerics to string. You can change it by setting the Thread.CurrentCulture property. Its original value is governed by the OS's regional settings Format property
  • The system locale is used by non-Unicode applications or when writing to ASCII files and the console.

You can also take advantage of WPF data binding and use InputLanguage as an attached property. In your XAML you can add the InputLanguageManager.InputLanguage property to an element's declaration like this:

<TextBox InputLanguageManager.InputLanguage="en-US"></TextBox>

You can then bind the property to a property in your code-behind or you ViewModel. Eg.

<TextBox InputLanguageManager.InputLanguage="{Binding MyLanguageInfo}"></TextBox>

Setting this property to a specific value will cause the keyboard of the UI element to change:

MyLanguageInfo = new CultureInfo("en-US");

or

MyLanguageInfo = new CultureInfo("el-GR");

You can go further with this and bind the InputLanguage property other elements, eg. a listbox of language options

于 2013-07-03T11:56:34.960 回答
2

For Keyboard Layout Changing are you InputLanguageManager on the right way.

InputLanguageManager.SetInputLanguage(this,CultureInfo.CreateSpecificCulture("ru"));

With the first Parameter of the SetInputLanguage() methode you set the DependencyObject which is the target of your Keyboard Layout.

于 2013-07-03T11:56:51.717 回答