WPF父菜单项调用子menuitem的命令

时间:2012-05-31 11:37:49

标签: c# .net wpf xaml

我的WPF / Xaml应用程序有一个带有文件和编辑菜单项的顶级菜单。文件menuitem包含Save and Exit menuitems。只有Save项绑定到ICommand。 “编辑”菜单为空。

当我第一次单击File以展开菜单时,会错误地调用SaveCommand.CanExecute方法。当我然后选择保存方法时,再次调用CanExecute(之后调用Execute)。

当我随后单击File时,不会执行CanExecute。当我再次单击Save,CanExecute和Execute被正确调用。

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

如何阻止File menuitem第一次调用SaveCommand.CanExecute?也许我的代码或管道(绑定)不正确?

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

<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。

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);
  }
 }
}
}

这是命令。

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 Framework 4和VS2010。

2 个答案:

答案 0 :(得分:2)

  

当我第一次单击File以展开菜单时,会错误地调用SaveCommand.CanExecute方法。

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

第一次显示时需要调用CanExecute方法,因为它会显示为灰色或不显示,具体取决于结果。

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

答案 1 :(得分:1)

您可以尝试RoutedUICommands:

    <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(object sender,CanExecuteRoutedEventArgs e)         {             e.CanExecute = true;         }

    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)
    {

    }