带子菜单的WPF菜单灰色显示已禁用 [英] WPF Menu with submenu Greyed out Disabled

查看:122
本文介绍了带子菜单的WPF菜单灰色显示已禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩WPF和菜单,我的菜单中有一个嵌套的MenuItem。我知道我所拥有的装饰是荒谬的,但它只是为了学习。我的问题是嵌套的Command =ApplicationCommands.New,并且Command =ApplicationCommands.Open总是被禁用而且不可选择我是否必须更改上下文?我用谷歌搜索寻找答案,但我读过的任何内容都没有解释。我试图弄清楚为什么他们会变灰,以及是什么导致他们变得灰暗。 xaml和我的代码隐藏的代码发布在下面。这只是一个菜单沙箱,请原谅评论谢谢。





我的XAML MARKUP

 <   window     x:class   =  WPF_Menus.MainWindow    xmlns:x   = #unknown >  
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题=MainWindow高度=350宽度=525>
< stackpanel >
< 菜单 >
< menuitem header = 文件 >
<! - 装饰菜单项带图像的新游戏还可以设置IsChecked或IsCheckable属性。
菜单还包含键盘快捷键的区域,但仍需要在后面的代码中设置它们。
- >

< menuitem < span class =code-attribute> header = 新游戏 单击 = MenuItemNewGame_Click inputgesturetext = Alt + N >
< menuitem.icon >
< 图片 来源 = Card.jpg / >
< / menuitem.icon >
< < span class =code-leadattribute> / menuitem >
<! - 作为此案例中的菜单标题的其他内容示例图像 - >
< menuitem inputgesturetext = Alt + Q >
< menuitem.header >
< 图片 来源 = Ace.jpg / >
< / menuitem.header >
< / menuitem > ;
<! -
附带命令的MenuItem。好处是文本标题是自动设置的,
键盘快捷键用相应的字符串填充,如果用户使用MenuItem或键盘快捷键执行命令,则命令被连接起来
。假设您已在后面的代码中正确绑定命令的
。我已将其设为嵌套MenuItem。
- >

< ; menuitem header < span class =code-keyword> = 文件 >
< menuitem 命令 = ApplicationCommands.New / >
< menuitem 命令 = ApplicationCommands.Open / >
< / menuitem >
< menuitem header = 退出 单击 = MenuItemExit_Click / >
< / menuitem &g t;
< menuitem header = 帮助 单击 = MenuItemHelp_Click / >
< / menu >
< / stackpanel >
< / window >





我的代码背后

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Data;
使用 System.Windows.Documents;
使用 System.Windows.Input;
使用 System.Windows.Media;
使用 System.Windows.Media.Imaging;
使用 System.Windows.Navigation;
使用 System.Windows.Shapes;

命名空间 WPF_Menus
{
/// < 摘要 >
/// MainWindow.xaml $ b的交互逻辑$ b /// < < span class =code-summarycomment> / summary >
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();

// 嵌套MenuItem的新建和开放的绑定
CommandBinding nBinding = new CommandBinding();
nBinding.Command = ApplicationCommands.New;
nBinding.Executed + = DoNew_Executed;
nBinding.CanExecute + = DoNew_CanExecute;

CommandBinding oBinding = new CommandBinding();
oBinding.Command = ApplicationCommands.Open;
oBinding.Executed + = DoOpen_Executed;
oBinding.CanExecute + = DoOpen_CanExecute;
}

// 常规菜单事件处理程序
private void MenuItemNewGame_Click( object sender, RoutedEventArgs e)
{
MessageBox.Show( Clicked New Game 菜单信息);
}
私有 void MenuItemExit_Click( object sender,RoutedEventArgs e)
{
MessageBox.Show( Clicked退出 菜单信息);
}
私有 void MenuItemHelp_Click( object sender,RoutedEventArgs e)
{
MessageBox.Show( Clicked帮助 菜单信息);
}

// 嵌套MenuItem的New和Open的事件处理程序。
public void DoNew_Executed( object sender,ExecutedRoutedEventArgs e)
{
MessageBox.Show( 执行新命令 命令信息);
}
public void DoNew_CanExecute( object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = true ;
}
public void DoOpen_Executed( object sender,ExecutedRoutedEventArgs e)
{
MessageBox.Show( Open命令执行 命令信息);
}
public void DoOpen_CanExecute( object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = true ;
}
}
}

解决方案

当您使用命令绑定时,您正在处理事件 System.Windows.Input.CommandBinding.CanExecute 并获取 System.Windows.Input.CanExecuteRoutedEventArgs 传递给处理程序。在处理程序中,您可以为布尔属性 CanExecute 指定一个值。 UI调用此事件并找出如何处理 UIElement System.Windows.UIElement.IsEnabled c $ c>参加了通讯记录。当然,如果你指定false(命令不应该执行),绑定的菜单项将被禁用。



请参阅: http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.canexecute。 aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/system.windows.input.canexecuteroutedeventargs.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system .windows.input.canexecuteroutedeventargs.canexecute.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.uielement.isenabled.aspx [ ^ ]。







而且,为了完成所有工作,您忘记将绑定对象添加到UI。您还需要 Window.CommandBindings.Add 。请参阅,例如:

http:// msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx [ ^ ]。



-SA


所以在谢尔盖的帮助下我发现了这一点。每个命令绑定我缺少一行,将绑定添加到绑定列表。我发布了绑定代码和下面的额外行。



//嵌套MenuItem的新建和打开的绑定

CommandBinding nBinding = new CommandBinding();

nBinding.Command = ApplicationCommands.New;

nBinding.Executed + = DoNew_Executed;

nBinding.CanExecute + = DoNew_CanExecute;

this.CommandBindings.Add(nBinding);



CommandBinding oBinding = new CommandBinding();

oBinding .Command = ApplicationCommands.Open;

oBinding.Executed + = DoOpen_Executed;

oBinding.CanExecute + = DoOpen_CanExecute;

this.CommandBindings.Add (oBinding);

I am playing around with WPF and menus and I have a nested MenuItem in my menu. I know the adorners I have are ridiculous but it's just for learning. The question I have is that nested Command="ApplicationCommands.New", and Command="ApplicationCommands.Open" are always disabled and not selectable do I have to change the context? I googled looking for an answer but nothing I have read explains it yet. I am trying to figure out why they are greyed out and what is causing them to be greyed out. The code for the xaml and my codebehind are posted below. It is just a Menu sandbox so excuse the comments Thanks.


MY XAML MARKUP

<window x:class="WPF_Menus.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <stackpanel>
        <menu>
            <menuitem header="File">
                <!--Adorned Menu Item New Game with an Image Can also set the IsChecked or IsCheckable Property.
                    Menus also contain and area for Keyboard shortcuts but they still need to be setup in the code behind.-->
                <menuitem header="New Game" click="MenuItemNewGame_Click" inputgesturetext="Alt+N">
                    <menuitem.icon>
                        <Image Source="Card.jpg"/>
                    </menuitem.icon>
                </menuitem>
                <!--Example of Other content as the Menu Header in this Case an Image-->
                <menuitem inputgesturetext="Alt+Q">
                    <menuitem.header>
                        <Image Source="Ace.jpg"/>
                    </menuitem.header>
                </menuitem>
                <!--MenuItem with Commands Attached. The Benefits are that the Text Header is automatically set,
                    The keyboard shortcut is filled with the appropriate string, and the command is hooked up so
                    if the user uses the MenuItem or the keyboard shortcut the command is executed. This assumes
                    that you've bound the command correctly in the code behind. I've made this a Nested MenuItem.-->
                <menuitem header="File">
                    <menuitem command="ApplicationCommands.New" />
                    <menuitem command="ApplicationCommands.Open" />
                </menuitem>
                <menuitem header="Exit" click="MenuItemExit_Click" />
            </menuitem>
            <menuitem header="Help" click="MenuItemHelp_Click" />
        </menu>
    </stackpanel>
</window>



MY CODE BEHIND

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF_Menus
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Bindings for the Nested MenuItem's New and Open
            CommandBinding nBinding = new CommandBinding();
            nBinding.Command = ApplicationCommands.New;
            nBinding.Executed += DoNew_Executed;
            nBinding.CanExecute += DoNew_CanExecute;

            CommandBinding oBinding = new CommandBinding();
            oBinding.Command = ApplicationCommands.Open;
            oBinding.Executed += DoOpen_Executed;
            oBinding.CanExecute += DoOpen_CanExecute;
        }

        //Regular Menu Event Handlers
        private void MenuItemNewGame_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Clicked New Game", "Menu Info");
        }
        private void MenuItemExit_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Clicked Exit", "Menu Info");
        }
        private void MenuItemHelp_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Clicked Help", "Menu Info");
        }

        //Event handlers for the Nested MenuItem's New and Open.
        public void DoNew_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("New Command Executed", "Command Info");
        }
        public void DoNew_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        public void DoOpen_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Open Command Executed", "Command Info");
        }
        public void DoOpen_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}

解决方案

As you use the command binding, you are handling the event System.Windows.Input.CommandBinding.CanExecute and get System.Windows.Input.CanExecuteRoutedEventArgs passed to the handler. In the handler, you can assign a value to the Boolean property CanExecute. The UI invokes this event and finds out what to do with the the property System.Windows.UIElement.IsEnabled for the instances of the UIElement participated in the correspondent binding. Naturally, if you assign false (the command should not execute), the bound menu item is disabled.

Please see: http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.canexecute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.canexecuteroutedeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.canexecuteroutedeventargs.canexecute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.isenabled.aspx[^].

[EDIT]

And, to put it all to work, you forgot to add binding object to your UI. You also need Window.CommandBindings.Add. Please see, for example:
http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx[^].

—SA


So with help from Sergey I figured this out. I was missing one line per each command binding that adds the binding to the binding list. I posted Just the binding code with the extra line below.

//Bindings for the Nested MenuItem's New and Open
CommandBinding nBinding = new CommandBinding();
nBinding.Command = ApplicationCommands.New;
nBinding.Executed += DoNew_Executed;
nBinding.CanExecute += DoNew_CanExecute;
this.CommandBindings.Add(nBinding);

CommandBinding oBinding = new CommandBinding();
oBinding.Command = ApplicationCommands.Open;
oBinding.Executed += DoOpen_Executed;
oBinding.CanExecute += DoOpen_CanExecute;
this.CommandBindings.Add(oBinding);


这篇关于带子菜单的WPF菜单灰色显示已禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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