本文介绍了如何启用/禁用XAML MenuItem? CommandBinding问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单:在XAML中配置菜单项,并控制是否在C#中启用或禁用该菜单项.菜单项未附加到控件.

"{}"内的CommandBinding内容仅给我以下错误:1)名称空间表示"中不存在SolutionMenuCmd,2)找不到类型"SolutionMenuCmd".它的语法是什么?

我是否必须按照下面的Microsoft示例所示定义自定义名称空间?

XAML代码

The problem is simple: configure a menu item in XAML and control whether it is enabled or disabled in C#. The menu item is not attached to a control.

The CommandBinding contents, inside the "{}" only give me errors 1) SolutionMenuCmd does not exist in namespace ''presentation'', 2) type ''SolutionMenuCmd'' not found. What is its syntax?

Do I have to define a custom namespace as Microsoft''s sample showed, below?

XAML Code

<Window x:Class="WpfCryptogramApplication.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Cipher Quote Assist" Height="400" Width="550">
    <Window.CommandBindings>
        <CommandBinding Command="{SolutionMenuCmd}"

                        Executed="MenuSolution_Executed"

                        CanExecute="MenuSolution_CanExecute"/>
    </Window.CommandBindings>
    <DockPanel LastChildFill="True">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="Add _Solution" Command="SolutionMenuCmd" />
            </MenuItem>
        </Menu>
    </DockPanel>
</Window>



C#代码



C# Code

public static RoutedCommand SolutionMenuCmd = new RoutedCommand();

private void MenuSolution_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (e.Source is MenuItem)
    {
        e.CanExecute = m_object.GameNumber == 0;
    }
}
private void MenuSolution_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Clicked the menu", "MenuSolution_Executed");
}


Microsoft的示例代码


Microsoft''s Sample Code

     xmlns:custom="clr-namespace:Custom_RoutedCommand"

<pre>    <CommandBinding Command="{x:Static custom:MainWindow.ColorCmd}"
                    Executed="ColorCmdExecuted"
                    CanExecute="ColorCmdCanExecute"/>





推荐答案

xmlns:MyNamespace="clr-namespace:My.Namespace"


Command="MyNamespace.MyClass.MyCommand"


您可以像上面一样省略x:Static


You can omit the x:Static like above


xmlns:MyNamespace="clr-namespace:MyNamespace"


这篇关于如何启用/禁用XAML MenuItem? CommandBinding问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:21
查看更多