2
votes

I create a pretty standard MVVM Light RelayCommand, with the canExecute parameter being:

() => (customer.Regions.Count > 0)

when necessary I call RaiseCanExecuteChanged(); and the canExecute is not reevaluated correctly. I then also call it manually like this: var canExecute = myCommand.CanExecute(null);, and it is still wrong. In the next statement right after I do var foo = (customer.Regions.Count > 0), which returns the correct result. What is going on here?

Command definition:

SaveCustomerRegionsCommand = new RelayCommand(SaveCustomerRegions, () => (customer.Regions.Count > 0));

customer is a private property set when the class is instantiated.

1
This is really odd, everything looks right. Have you tried creating a CanExecute method to avoid using a lambda expression? I know it's a longshot, but when you are stuck everything is worth a try. - Hannish
Yeah it works with a method, but I have other commands that work just fine with lambdas. If I use a relaycommand I created myself, it works just fine, it's just the MVVM Light one that fails. _canExecute has an internal property IsAlive, which is false. maybe this is the reason? then the question becomes why is it false.. - user1151923
can you share code where you are defining your relay command, I mean method... - User1551892
You should raise a different question regarding this behavior of MVVM Light's RelayCommand. Since my suggestion is working I'm posting it as an answer, hope you are ok with it to calify it as the solution. Regards - Hannish
Have you tried () => true? Is it working? If its working then replace the lambda expression with something else. - User1551892

1 Answers

0
votes

(from comment) Create a CanExecute method to avoid using a lambda expression.

Looks that the IsAlive property of the MVVM Light Framework is interferring somehow, as you suggest. You can post another question regarding that if you like. Regards!