简体   繁体   English

在WPF中更改一个/活动窗口的系统语言

[英]Change system language for one/active window in WPF

Is there a posibility to change system language only for one window in WPF? 是否只能在WPF中为一个窗口更改系统语言?

I know about InputLanguageManager but I assume that it changes language in whole system. 我知道InputLanguageManager但我认为它改变了整个系统的语言。

InputLanguageManager does exactly what you are asking for. InputLanguageManager完全符合您的要求。 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. 如果您打开记事本并切换到俄语,打开IE并切换到英语,当您激活记事本应用程序时,您的键盘区域设置仍将是俄语。

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: .NET应用程序中使用了三种不同的语言环境:

  • The UI locale is the locale used to display messages and select localized UI strings and layouts. UI语言环境是用于显示消息并选择本地化UI字符串和布局的语言环境。 You can change a thread's UI locale by setting its Thread.CurrentUICulture property. 您可以通过设置Thread.CurrentUICulture属性来更改线程的UI语言环境。 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. 您可以通过设置Thread.CurrentCulture属性来更改它。 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. 系统区域设置由非Unicode应用程序使用,或者在写入ASCII文件和控制台时使用。

You can also take advantage of WPF data binding and use InputLanguage as an attached property. 您还可以利用WPF数据绑定并使用InputLanguage作为附加属性。 In your XAML you can add the InputLanguageManager.InputLanguage property to an element's declaration like this: 在您的XAML中,您可以将InputLanguageManager.InputLanguage属性添加到元素的声明中,如下所示:

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

You can then bind the property to a property in your code-behind or you ViewModel. 然后,您可以将该属性绑定到代码隐藏中的属性或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: 将此属性设置为特定值将导致UI元素的键盘更改:

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. 您可以更进一步,并将InputLanguage属性绑定到其他元素,例如。 a listbox of language options 语言选项的列表框

For Keyboard Layout Changing are you InputLanguageManager on the right way. 对于键盘布局,您可以通过正确的方式更改InputLanguageManager

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. 使用SetInputLanguage()方法的第一个参数,您可以设置DependencyObject ,它是键盘布局的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM