首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF父menuitem调用子menuitem的命令

WPF父menuitem调用子menuitem的命令
EN

Stack Overflow用户
提问于 2012-05-31 19:37:49
回答 2查看 1.7K关注 0票数 2

我的WPF / Xaml应用程序有一个带有文件和编辑菜单项的顶级菜单。文件menuitem包含保存和退出菜单项。只有保存项才会绑定到ICommand。“编辑”菜单为空。

当我第一次单击“文件”展开菜单时,错误地调用了SaveCommand.CanExecute方法。然后,当我选择Save时,方法CanExecute被再次调用(之后调用Execute )。

当我随后单击文件时,CanExecute不会被执行。当我再次单击保存时,CanExecute和Execute被正确调用。

单击编辑或退出不会触发CanExecute,也不会执行。

如何防止文件菜单项第一次调用SaveCommand.CanExecute?也许我的代码或管道(绑定)是不正确的?

下面是MainWindow.xaml (MainWindow.xaml.cs只包含自动生成的代码)

代码语言:javascript
复制
<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApp.ViewModels"
    Title="Blah" Height="350" Width="525">

    <Window.DataContext>
    <local:MyFirstViewModel />
</Window.DataContext>

<Menu>
    <MenuItem Header="File" Name="mnItmFile" >
        <MenuItem Name="mnItmSave" Header="Save"  Command="{Binding Path=SaveCommand}"/>
        <MenuItem Header="Exit"/>
    </MenuItem>
    <MenuItem Header="Edit"/>
</Menu>
</Window>

下面是ViewModel。

代码语言:javascript
复制
using System;
using System.Windows.Input;
using System.ComponentModel;

namespace MyApp.ViewModels
{
 public class MyFirstViewModel : INotifyPropertyChanged
 {
public ICommand SaveCommand
{
  get { return new MyFirstCommand(); }
}

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
  PropertyChangedEventHandler handler = this.PropertyChanged;
  if (handler != null)
  {
    var e = new PropertyChangedEventArgs(propertyName);
    handler(this, e);
  }
 }
}
}

下面是命令。

代码语言:javascript
复制
using System;
using System.Windows.Input;

namespace MyApp.Commands
{
 public class MyFirstCommand : ICommand
{
private static int i = 0; //debug helper
public bool CanExecute(object parameter)
{
  i++; //count num of times this method is called
  return true;
}

public void Execute(object parameter)
{
  //do some stuff
  int x = 5;
  x++;
}
public event EventHandler CanExecuteChanged;

}
}

我正在使用.NET Framework4和VS2010。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-31 19:50:56

当我第一次单击“文件”展开菜单时,错误地调用了SaveCommand.CanExecute方法。

不,这是预期行为。单击“文件”菜单时,将显示所有子菜单项,包括“保存”菜单项。“保存”菜单项在您第一次单击“文件”菜单后调用CanExecute方法,因为“保存”菜单项是第一次显示。

它需要在第一次显示时调用CanExecute方法,因为它是否灰显取决于结果。

之后,每当您单击“文件”菜单时,它不需要再次调用CanExecute,因为它已经知道第一次的结果。除非您的ICommand引发CanExecuteChanged事件,否则它不会更改。

票数 2
EN

Stack Overflow用户

发布于 2012-05-31 21:20:08

您可以尝试RoutedUICommands:

代码语言:javascript
复制
    <Window x:Class="Combobox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:Combobox">
<Window.DataContext>
    <local:MyFirstViewModel />
</Window.DataContext>
<Window.CommandBindings>
    <CommandBinding Command="Save" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"></CommandBinding>
    <CommandBinding Command="Close" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_1"></CommandBinding>
</Window.CommandBindings>
    <Menu>
    <MenuItem Header="File" Name="mnItmFile" >
        <MenuItem Name="mnItmSave" Header="Save"  Command="Save"/>
        <MenuItem Header="Exit" Command="Close"/>
    </MenuItem>
    <MenuItem Header="Edit"/>
</Menu>

.private void CommandBinding_CanExecute(对象发送者,CanExecuteRoutedEventArgs e) { e.CanExecute = true;}

代码语言:javascript
复制
    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {

    }

    private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
    {

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10832817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档