简体   繁体   English

WPF-从背后的代码打开/关闭键盘

[英]WPF - Turn on/off keyboard from code behind

There's a custom touchscreen keyboard in my app built according to this: https://www.codeproject.com/Articles/32568/A-Touch-Screen-Keyboard-Control-in-WPF . 我的应用程序中有一个自定义触摸屏键盘,它是根据以下方式构建的: https : //www.codeproject.com/Articles/32568/A-Touch-Screen-Keyboard-Control-in-WPF I also have a ResourceDictionary containing all styles and templates. 我还有一个ResourceDictionary,其中包含所有样式和模板。 In the TextBox style, I can set the keyboard ON/OFF: 在TextBox样式中,我可以将键盘设置为ON / OFF:

<Style TargetType="{x:Type TextBox}">     
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Padding" Value="4" />
    <Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="True"/>
</Style>

I would like the user to be able to turn it on or off from the UI, but can't figure out how to reach this property from code behind. 我希望用户能够从UI启用或禁用它,但无法弄清楚如何从后面的代码访问此属性。 I would like to make it without naming the style, since it's pretty commonly used throughout the app. 我想在不命名样式的情况下制作它,因为它在整个应用程序中都很常用。 I tried this, but (no surprise) get ArgumentNotFoundException: 我试过了,但是(毫不奇怪)得到ArgumentNotFoundException:

Style s = Application.Current.FindResource("defTextBox") as Style;
s.RegisterName("Keyboard.TouchScreenKeyboard.TouchScreenKeyBoard",false);

Any help would be appreciated! 任何帮助,将不胜感激!

You shoud be able to set the TouchScreenKeyboard attached property for an individual TextBox like this: 您应该能够为单个TextBox设置TouchScreenKeyboard附加属性,如下所示:

TouchScreenKeyboard.SetTouchScreenKeyboard(textBox1, false);

Changing the defintion of the implicit Style itself after it has already been applied to all TextBox elements doesn't make much sense though. 但是,在将隐式Style本身的定义应用于所有TextBox元素之后,对其进行更改并不是很有意义。 You should define the default value in XAML and then change the value for individual TextBoxes dynamically at runtime if you need to. 您应该在XAML中定义默认值,然后根据需要在运行时动态更改各个TextBoxes的值。

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

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