是否可以仅为 WPF 中的一个窗口更改系统语言?
我知道,InputLanguageManager
但我认为它会改变整个系统的语言。
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:
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
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.