本文介绍了在RichTextBox中禁用编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我想禁用所有RichTextBox编辑,但不使用IsReadOnly,因为稍后我将手动处理一些自定义编辑.

I want to disable all RichTextBox editing, but without using IsReadOnly, because later I will handle some custom editing manually.

我已将以下内容附加到PreviewTextInput事件:

I've attached the following to the PreviewTextInput event:

私有 无效 OnPreviewTextInput( 对象 发件人, TextCompositionEventArgs e){

private void OnPreviewTextInput(object sender, TextCompositionEventArgs e) {

e.Handled = true ;

e.Handled = true;

}

并在InitializeComponent之后在窗口构造函数中调用DisableNonReadOnlyCommands(textBox):

and I call DisableNonReadOnlyCommands(textBox) in the window constructor after InitializeComponent:

public 静态 void DisableNonReadOnlyCommands( RichTextBox textBox){

public static void DisableNonReadOnlyCommands(RichTextBox textBox) {

foreach ( RoutedUICommand 命令 nonReadOnlyCommands){

foreach (RoutedUICommand command in nonReadOnlyCommands) {

CommandManager .RegisterClassCommandBinding(textBox.GetType(),

CommandManager.RegisterClassCommandBinding(textBox.GetType(),

CommandBinding (

new CommandBinding(

命令

ExecutedRoutedEventHandler (NullOnCommand),

new ExecutedRoutedEventHandler(NullOnCommand),

CanExecuteRoutedEventHandler (NullOnCanExecuteCommand)));

new CanExecuteRoutedEventHandler(NullOnCanExecuteCommand)));

}

}

私有 静态 无效 NullOnCommand( 对象 发送者, ExecutedRoutedEventArgs e){

private static void NullOnCommand(object sender, ExecutedRoutedEventArgs e) {

e.Handled = true ;

e.Handled = true;

}

私有 静态 无效 NullOnCanExecuteCommand( 对象 发送者, CanExecuteRoutedEventArgs e){

private static void NullOnCanExecuteCommand(object sender, CanExecuteRoutedEventArgs e) {

e.CanExecute = false ;

e.CanExecute = false;

e.Handled = true ;

e.Handled = true;

}

私有 静态 只读 RoutedUICommand [] nonReadOnlyCommands = RoutedUICommand [] {

private static readonly RoutedUICommand[] nonReadOnlyCommands = new RoutedUICommand[] {

EditingCommands .AlignCenter,

EditingCommands.AlignCenter,

EditingCommands .AlignJustify,

EditingCommands.AlignJustify,

EditingCommands .AlignLeft,

EditingCommands.AlignLeft,

EditingCommands .AlignRight,

EditingCommands.AlignRight,

EditingCommands .Backspace,

EditingCommands.Backspace,

EditingCommands .CorrectSpellingError,

EditingCommands.CorrectSpellingError,

EditingCommands .DecreaseFontSize,

EditingCommands.DecreaseFontSize,

EditingCommands .DecreaseIndentation,

EditingCommands.DecreaseIndentation,

EditingCommands .删除

EditingCommands.Delete,

EditingCommands .DeleteNextWord,

EditingCommands.DeleteNextWord,

EditingCommands .DeletePreviousWord,

EditingCommands.DeletePreviousWord,

EditingCommands .EnterLineBreak,

EditingCommands.EnterLineBreak,

EditingCommands .EnterParagraphBreak,

EditingCommands.EnterParagraphBreak,

EditingCommands .IgnoreSpellingError,

EditingCommands.IgnoreSpellingError,

EditingCommands .IncreaseFontSize,

EditingCommands.IncreaseFontSize,

EditingCommands .IncreaseIndentation,

EditingCommands.IncreaseIndentation,

EditingCommands .ToggleBold,

EditingCommands.ToggleBold,

EditingCommands .ToggleBullets,

EditingCommands.ToggleBullets,

EditingCommands .ToggleInsert,

EditingCommands.ToggleInsert,

EditingCommands .ToggleItalic

EditingCommands.ToggleItalic,

EditingCommands .ToggleNumbering,

EditingCommands.ToggleNumbering,

EditingCommands .ToggleSubscript,

EditingCommands.ToggleSubscript,

EditingCommands .ToggleSuperscript,

EditingCommands.ToggleSuperscript,

EditingCommands .ToggleUnderline,

EditingCommands.ToggleUnderline,

ApplicationCommands .Cut,

ApplicationCommands.Cut,

ApplicationCommands .删除

ApplicationCommands.Delete,

ApplicationCommands .粘贴

ApplicationCommands.Paste,

};

问题是Delete和Control + X仍然有效.

The problem is that Delete and Control+X still work.

我在做什么错了?

最好的问候,

Gustavo Guerra

Gustavo Guerra

推荐答案


这篇关于在RichTextBox中禁用编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:28