Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:59:48+00:00 2026-06-04T23:59:48+00:00

my WPF / Xaml application has a top level menu with File and Edit

  • 0

my WPF / Xaml application has a top level menu with File and Edit menuitems. The file menuitem contains Save and Exit menuitems. Only the Save item is bound to an ICommand. The Edit menu is empty.

When I click File the FIRST time to expand the menu, the SaveCommand.CanExecute method is incorrectly called. When I then select Save the method CanExecute is called again (after which Execute is called).

When I click File subsequently the CanExecute is not executed. And when I again click Save, CanExecute and Execute are correctly called.

Clicking Edit or Exit does not trigger CanExecute nor Execute.

How do I prevent the File menuitem calling SaveCommand.CanExecute the first time ? Perhaps my code or the plumbing (binding) are incorrect ?

Below is MainWindow.xaml (MainWindow.xaml.cs contains only the auto-generated code)

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

Below is the 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);
  }
 }
}
}

And here is the command.

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;

}
}

I’m using .NET Framework 4 and VS2010.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:59:50+00:00Added an answer on June 4, 2026 at 11:59 pm

    When I click File the FIRST time to expand the menu, the SaveCommand.CanExecute method is incorrectly called.

    Nope, this is expected behavior. When you click the “File” Menu, it displays all the child menu items, including the “Save” menu item. The “Save” menu item is calling the CanExecute method after you click the “File” Menu for the first time, because the “Save” menu item is being displayed for the first time.

    It needs to call the CanExecute method when it’s displayed for the first time, because it will either be greyed-out or not, depending on the result.

    After that, whenever you click the “File” menu, it doesn’t need to call CanExecute again, because it already knows the result from the first time. It won’t change unless your ICommand raises the CanExecuteChanged event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF/C# 4.0 App which has an Application file XAML that is:
Guys, I have a basic WPF application. Contains App.xaml as always and a Mainwindow.xaml.
In my WPF application, I have a main window (Branch.xaml) which has a button
I want to build a WPF Application that, when started, only has a tray
I have a WPF application which has a main window composed from several custom
In a WPF Application using XAML, I created a stackpanel(width 1030) and I have
I have a C# WPF .NET 4 application that has an icon in the
I'm working on a WPF application that has no main window (it runs in
I have the following binding in my wpf application xaml: <TextBox Text={Binding Amount, StringFormat=c}
I'm developing a WPF/PRISM based application. The parent view(usercontrol) has many regions in it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.