WPF——如何禁用自定义 Richtextbox 上的默认 EditingCommands 行为?

发布于 2024-08-26 13:14:25 字数 71 浏览 4 评论 0原文

我想禁用“EditingCommands”中 WPF 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2024-09-02 13:14:25

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

您可以通过添加 CommandBinding 来禁用任何默认启用的命令。在下面的代码片段中,属性 CanExecute 引用了一个事件处理程序,该事件处理程序可以合并逻辑以阻止命令执行:

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

C# 代码隐藏文件中相应的事件处理程序在事件对象上设置两个属性。将 CanExecute 设置为 false 可以让绑定到该事件的组件知道该选项当前不可用。在这种情况下,要阻止 ToggleBold 命令,请将 CanExecute 设置为 false 会禁用工具栏中的粗体按钮,使其显示为禁用状态。第二个属性 Handled 可防止 ToggleBold 命令路由到 RichTextBox:

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

如果您选择在 CanExecute 处理程序中合并复杂的逻辑,请记住此事件经常被触发,因为 UI 会不断检查并重新检查该命令是否可用。如果您需要访问数据库或 Web 服务等资源来确定该命令是否可用,请确保缓存该资源并仅定期检查它,否则会破坏您的性能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文