命令绑定不工作? [英] commandbinding not working?

查看:155
本文介绍了命令绑定不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多标签应用程序(例如:多标签文本编辑器),其中每个标签项都有自己的内容。在tabitem的上下文菜单中,他们是一个命令的菜单,说SelectAll命令。



运行应用程序后,菜单项始终禁用,不执行命令。



那么,我如何让我的命令绑定工作?



CODE ::



在上下文菜单中TextEditor>

 < MenuItem Command =local:TextEditor.SelectAllCommand Header =全选/> 

在CommandBindings中TextEditor>

 < UserControl.CommandBindings> 
< CommandBinding Command =local:TextEditor.SelectAllCommand
Executed =SelectAll_ExecutedCanExecute =SelectAll_CanExecute/>
< /UserControl.CommandBindings>

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



使用像这样

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

 < MenuItem Header =CutCommand =CutCommandTarget =
{Binding Path = PlacementTarget,
RelativeSource = {RelativeSource FindAncestor,
AncestorType = {x:Type ContextMenu}}}/>

如需更多信息,请查看以下链接



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


I working on a multi-tab application (For Ex: Multi-Tab Text Editor), where each tabitem has its own content. And in contextmenu of tabitem, their is menuitem with a command, say SelectAll command.

After running app, the menu item is always disabled, no command execution is done.

So, how can i make my commandbindings work ?

CODE ::

In Context Menu At TextEditor>

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

In CommandBindings At TextEditor>

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

The TabItems with TextEditor are created at run time

解决方案

This happens since the ContextMenus are separate windows with their own VisualTree and LogicalTree.

Use like this

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

For more check the link below

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

这篇关于命令绑定不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆