无法将菜单项中的命令绑定到命令绑定

无法将菜单项中的命令绑定到命令绑定

本文介绍了无法将菜单项中的命令绑定到命令绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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.关闭菜单项始终处于禁用状态.

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?

推荐答案

此语句Command="Close"不执行任何操作.您说的是Command是字符串"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.

如果在窗口中定义了关闭命令实例,请使用Command="{Binding Close}".或者,如果您使用的是ApplicationCommands.Close,则为

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

Command="{x:Static ApplicationCommands.Close}"

这篇关于无法将菜单项中的命令绑定到命令绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:22