开发者

WPF -- How can I disable the default EditingCommands behavior on my custom richtextbox?

I want to disable the majority of the built-in functionality of the WPF Richtextbox that is found in "EditingCommands"

How开发者_运维百科 can I go about disabling them?


http://www.devx.com/dotnet/Article/34644/1954 :

You can disable any of the default enabled commands by adding a CommandBinding. In the snippet below, the attribute CanExecute references an event handler that can incorporate logic to prevent the command from executing:

<RichTextBox.CommandBindings>
     <CommandBinding 
       Command="EditingCommands.ToggleBold" 
       CanExecute="BlockTheCommand"/>
   </RichTextBox.CommandBindings>

The corresponding event handler in the C# code-behind file sets two properties on the event object. Setting CanExecute to false lets components that are bound to the event know that this option is not currently available. In this case, to block the ToggleBold command, setting CanExecute to false disables the Bold button in the toolbar appear disabled. The second property, Handled, prevents the ToggleBold command from being routed to the RichTextBox:

protected void BlockTheCommand(object sender,
     CanExecuteRoutedEventArgs e)
   {
     e.CanExecute = false;
     e.Handled = true;
   }

If you choose to incorporate complex logic in your CanExecute handlers, keep in mind that this event gets fired often, because the UI constantly checks and re-checks to see if the command is available. If you need to access resources such as a database or a Web service to determine if the command should be available, make sure you cache the resource and check it only periodically, or it will destroy your performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜