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 6067959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:39:52+00:00 2026-05-23T09:39:52+00:00

I need help to figure out why my command doesn’t work on menu item.

  • 0

I need help to figure out why my command doesn’t work on menu item. I’ve been googling the solutions for this, and found few too in here. But probably because of my knowledge (beginner WPF), I still couldn’t solve it. Any help is appreciated!

It works with button, but not with the menu item.

XAML:

    <Window x:Class="WPFBeginner.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="349" Width="259">
    <Grid>
        <Grid.RowDefinitions />
        <Grid.ColumnDefinitions />
        <Menu Height="22" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="237" Margin="0,1,0,0">
            <MenuItem Header="_File" >
                <MenuItem Header="Save As" Command="{Binding SaveCommand}"/>
                <Separator />
                <MenuItem Command="Close" />
            </MenuItem>
            <MenuItem Header="_Edit">
                <MenuItem Command="Undo" />
                <Separator />
                <MenuItem Command="Cut" />
                <MenuItem Command="Copy" />
                <MenuItem Command="Paste" />
                <Separator />
                <MenuItem Command="SelectAll" />
            </MenuItem>
        </Menu>
        <TextBox Height="217" HorizontalAlignment="Left" Margin="0,21,0,0" Name="txtBox1" VerticalAlignment="Top" Width="238" 
                 Text="{Binding Note.Data}" />
        <!--button works fine-->
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="12,244,0,0" Name="button1" VerticalAlignment="Top" Width="75" 
                Command="{Binding SaveCommand}"/>
    </Grid>
</Window>

Here’s the code for the ViewModel.

    class NoteViewModel : INotifyPropertyChanged
{
    public ICommand SaveCommand { get; set; }

    public NoteViewModel()
    {
        SaveCommand = new RelayCommand(Save);
        Note = new NoteModel();
    }

    private NoteModel note;
    public NoteModel Note
    {
        get { return note; }
        set
        {
            if (note != value)
            {
                note = value;
                RaisedPropertyChanged("Note");
            }
        }
    }

    private void Save()
    {
        SaveFileDialog file = new SaveFileDialog();

        if ((bool)file.ShowDialog())
        {
            File.WriteAllText(file.FileName, Note.Data, Encoding.UTF8);
        }
    }

    #region ...INPC
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisedPropertyChanged(string p)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }
    #endregion
}

I debug it, it turned out that the command (SaveCommand –> Save()) is executed, but the value of Note.Data is null. It’s something if I use button instead.

EDIT:
Extra information: I use RelayCommand from MVVMLight.

  • 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-05-23T09:39:52+00:00Added an answer on May 23, 2026 at 9:39 am

    What’s likely happening is that the TextBox still has focus when you select your menu item. By default, bindings in WPF update when the control loses focus (so that updates don’t take place constantly, as they do when PropertyChange is the update type). When you use the button, the TextBox loses focus because the button gets it.

    You can test this out by putting another control (any kind) on the window and clicking on it before selecting your menu item.

    If this addresses the issue, then the simplest fix is to change the binding update type to PropertyChange (this can be done in the binding options area of the designer).

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

Sidebar

Related Questions

I'm trying to figure out how to work git filter-branch and I need help
Hey all i am in need of some help trying to figure out how
I need help trying to figure out the proper design of my classes. I
I need some help trying to figure out how to format dates in perl.
Can anyone help, i trying to figure what i need to do, i have
Need help writing a script downloads data from google insight using c# this is
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
I'm using a Google phone running Android, and I need to figure out how
I'm trying to figure out this problem for one of my comp sci classes,

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.