none
Problem with MenuItem Command Binding RRS feed

  • Question

  • Hi,

    I have a MenuItem like this

    <MenuItem Header="Kund" Command="{Binding SokKundCommand}"/>

    It does not fire SokKundCommand in my ModelView as a Button usually does. What is problem?

    Tuesday, March 4, 2014 11:42 AM

Answers

  • You should make the ICommand property public in order for the view to be able to bind to it:

    public ICommand SokKundCommand

    • Marked as answer by Muris Tuesday, March 4, 2014 1:39 PM
    Tuesday, March 4, 2014 1:18 PM

All replies

  • The command should get fired when you click on the MenuItem provided that the CanExecute method of the command returns true and that the DataContext of the MenuItem actually has an ICommand property named exactly "SokKundCommand".

    You need to post the code of your view model and preferably also the code of your ICommand implementation for further help.

    Tuesday, March 4, 2014 12:42 PM
  • Let's go

    I have 

    <MenuItem Header="Kund" Command="{Binding SokKundCommand}"/>

    within a windows that I did connect a my ViewMOdel as DataContext to. I have another string property that binds to my ViewModel so DataContext connection is ok.

    In my ViewModel I have declared following property

    private DelegateCommand sokKundCommand; private ICommand SokKundCommand { get { if (sokKundCommand == null) { sokKundCommand = new DelegateCommand(() => { this.OnClose(); }, () => true); } return sokKundCommand;

    } } public void OnClose() { System.Environment.Exit(0); }

    When I set a breakpoint within OnClose() method, it does not stop program....

    Is it something special with MenuItem commanding?

     




    • Edited by Muris Tuesday, March 4, 2014 1:12 PM
    Tuesday, March 4, 2014 1:09 PM
  • You should make the ICommand property public in order for the view to be able to bind to it:

    public ICommand SokKundCommand

    • Marked as answer by Muris Tuesday, March 4, 2014 1:39 PM
    Tuesday, March 4, 2014 1:18 PM