0

My application consists of a MainWindow and two pages which get loaded into a Frame via a simple Navigation service which uses static hard-coded Uris in the ViewModelLocator. This works fine, but I am seeing some oddities with RelayCommand.

One page contains a ListBox of items and an Edit button. If I select an item in the ListBox, the CanExecute method of the EditCommand fires and enables the button (because SelectedItem is not null). When I switch to the other Page in the App, and then switch back (without doing anything) - the SelectedItem is grayed out (Selected,Inactive). No problem with that as focus is on the Menu button. The thing I can't figure out is why the Edit button is Disabled (the CanExecute of the RelayCommand did not fire when the page was re-displayed - verified with debugger) since the SelectedItem still has the correct value.

The second issue I am seeing is that after switching between the pages in my app, the RelayCommand for the Edit button is firing multiple times when I change the Selected Item in the Listbox. I can confirm via the debugger that only one instance of the ViewModel exists (the constructor only got called once.)

So far I have observed the RelayCommand firing 3 times each time a change the SelectedItem.

Can anyone explain this behaviour and offer a solution?

Alan Rutter
  • 321
  • 4
  • 16
  • 6
    Your question is *crying out* for some code. Your grayed out Edit button could, conceivably, be a result of this known bug: http://blog.galasoft.ch/posts/2015/01/re-enabling-the-commandmanager-feature-with-relaycommand-in-mvvm-light-v5 . – goobering May 29 '15 at 07:15
  • I will try to produce a cut-down version of code that exhibits the same problem. In the meantime, I have switched my references to `Galasoft.MvvmLight.CommandWpf` instead of `Galasoft.MvvmLight.Command` and this has fixed the first issue. The second issue of the RelayCommand's CanExecute being fired multiple times (up to 4 times) when the selected item changes still exists. – Alan Rutter Jun 01 '15 at 21:01

0 Answers0