WPF 禁用TextBox的觸摸後自動彈出虛擬鍵盤

前言 & 問題

以下截圖,TextBox,在觸摸點擊後,會自動彈出windows的虛擬鍵盤。html

如何,禁用鍵盤的自動彈出?c#

調用虛擬鍵盤

經過調用TapTip.exe或者osk.exe,主動彈出虛擬鍵盤windows

詳細調用可參考:c#調用windows虛擬鍵盤ide

如何禁用鍵盤的彈出

TextBox在觸摸點擊後,會自動彈出虛擬鍵盤,是由於在控件中做了封裝。this

處理方案:重寫TextBox的方法OnCreateAutomationPeer,返回一個UIElementAutomationPeer而不是AutomationPeer。spa

可能緣由:TextBox自定義實現中返回的是AutomationPeer,而UIElementAutomationPeer繼承AutomationPeer,重寫了相關鍵盤屬性。code

猜想與HasKeyboardFocusCore屬性有關。htm

方案參考:blog

 1     /// <summary>
 2     /// 禁用自動彈出虛擬鍵盤的TextBox控件
 3     /// </summary>
 4     public class TextBoxNoAutoKeyboard : TextBox
 5     {
 6         protected override AutomationPeer OnCreateAutomationPeer()
 7         {
 8             return new FrameworkElementAutomationPeer(this);
 9         }
10     }

 以上參考自:【stackoverflow】「Hide」 text box from automatic Win10 keyboard showing繼承

相關文章
相關標籤/搜索