无法将命令从菜单项绑定到命令绑定
我有以下 xaml:
<Window x:Class="Isolator.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Isolator" Height="394" Width="486" Background="Black" WindowStyle="None" WindowState="Maximized">
<Window.CommandBindings>
<CommandBinding Command="Close" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
</Window.CommandBindings>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="Stop" Name="StopMenuItem" Click="StopMenuItem_Click" />
<MenuItem Header="Close" Command="Close"/>
</ContextMenu>
</Window.ContextMenu>
<Grid Loaded="Grid_Loaded">
</Grid>
</Window>
“关闭”菜单项指定它应该使用“关闭”命令。 Close 命令绑定指定应为 CanExecute 调用 CommandBinding_CanExecute,但 CommandBinding_CanExecute 永远不会被调用。 关闭菜单项始终处于禁用状态。
我认为绑定没有发生。 任何人都可以解释为什么吗?
如果它与上下文菜单不在可视化树中有关,您如何解决它?
I've got the following xaml:
<Window x:Class="Isolator.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Isolator" Height="394" Width="486" Background="Black" WindowStyle="None" WindowState="Maximized">
<Window.CommandBindings>
<CommandBinding Command="Close" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
</Window.CommandBindings>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="Stop" Name="StopMenuItem" Click="StopMenuItem_Click" />
<MenuItem Header="Close" Command="Close"/>
</ContextMenu>
</Window.ContextMenu>
<Grid Loaded="Grid_Loaded">
</Grid>
</Window>
The Close menu items specifies that it should use the Close command. The Close command binding specifies that CommandBinding_CanExecute should be called for CanExecute, but CommandBinding_CanExecute never gets called. The close menu item is always disabled.
I assume that the binding isn't taking place. Can any one explain why?
If it has something to do with context menus not being in the visual tree, how do you get work around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此语句
Command="Close"
不执行任何操作。 您说的命令是字符串“Close”。 这就是为什么它不起作用。如果在窗口中定义了 Close 命令实例,请使用
Command="{Binding Close}"
。 或者,如果您使用的是 ApplicationCommands.Close,那么它将是This statement
Command="Close"
doesn't do anything. You are saying that the Command is the string "Close". This is why it doesn't work.If the Close command instance is defined in the Window, use
Command="{Binding Close}"
. Or if you are using the ApplicationCommands.Close, then it would be