gpt4 book ai didi

wpf - 命令绑定(bind)不起作用?

转载 作者:行者123 更新时间:2023-12-04 00:47:24 27 4
gpt4 key购买 nike

我正在开发一个多选项卡应用程序(例如:多选项卡文本编辑器),其中每个 tabitem 都有自己的内容。在 tabitem 的 contextmenu 中,它们是带有命令的 menuitem,比如 SelectAll 命令。

运行应用程序后,菜单项始终处于禁用状态,不执行任何命令。

那么,我怎样才能使我的命令绑定(bind)工作?

代码::

在 TextEditor 的上下文菜单中>

<MenuItem Command="local:TextEditor.SelectAllCommand" Header="Select All" />

在 TextEditor 的 CommandBindings 中>
<UserControl.CommandBindings>
<CommandBinding Command="local:TextEditor.SelectAllCommand"
Executed="SelectAll_Executed" CanExecute="SelectAll_CanExecute" />
</UserControl.CommandBindings>

带有 TextEditor 的 TabItems 是在运行时创建的

最佳答案

这是因为 ContextMenus 是具有自己的 VisualTree 和 LogicalTree 的单独窗口。

像这样使用

<MenuItem Header="Cut" Command="Cut" CommandTarget="
{Binding Path=PlacementTarget,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ContextMenu}}}"/>

有关更多信息,请查看下面的链接

http://www.wpftutorial.net/RoutedCommandsInContextMenu.html

关于wpf - 命令绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6070478/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com