有没有办法禁用或覆盖 WinRT RichEditBox 控件上的键盘快捷键?我希望能够在您按下 Ctrl-B 和 Ctrl-I 时禁用粗体和斜体格式。
我避免使用常规的纯文本框,因为我想使用 RichEditBox 中的格式设置选项为文本添加语法高亮显示。如果用户可以在框中操纵样式,那将不起作用。
谢谢!
最佳答案
我终于在 another question 中找到了答案: 文本控件的 OnKeyDown
方法在 KeyDown
事件被触发之前被调用,所以不是监听 KeyDown
事件,你必须创建RichEditBox
的子类并覆盖 OnKeyDown
方法。然后在您的 XAML 标记中或在实例化 RichEditBox
的任何地方,改用您的自定义子类。作为一个有点相关的例子,我创建了一个 TextBox
的覆盖来防止撤销和重做操作:
[Windows::Foundation::Metadata::WebHostHidden]
public ref class BetterTextBox sealed : public Windows::UI::Xaml::Controls::TextBox
{
public:
BetterTextBox() {}
virtual ~BetterTextBox() {}
virtual void OnKeyDown(Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) override
{
Windows::System::VirtualKey key = e->Key;
Windows::UI::Core::CoreVirtualKeyStates ctrlState = Windows::UI::Core::CoreWindow::GetForCurrentThread()->GetKeyState(Windows::System::VirtualKey::Control);
if ((key == Windows::System::VirtualKey::Z || key == Windows::System::VirtualKey::Y) &&
ctrlState != Windows::UI::Core::CoreVirtualKeyStates::None)
{
e->Handled = true;
}
// only call the base implementation if we haven't already handled the input
if (!e->Handled)
{
Windows::UI::Xaml::Controls::TextBox::OnKeyDown(e);
}
}
};
关于c# - 覆盖 RichEditBox 上的键盘快捷键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28551418/