I'm implementing the CommunityToolkit.Mvvm framework on my WPF desktop application. I'm using the in-built RelayCommand with the 'CanExecute' coming from a navigation service helper class that I've written elsewhere.
Very simply I want the Button which is bound to the RelayCommand to update the CanExecute boolean when the navigation service is used to switch between views. On trying to raise the Command.NotifyCanExecuteChanged(); method, I am getting a System.StackOverflowException on loading the view?
public MainViewModel() {
_navigationServices = new NavigationServices();
this.PreviousViewCommand = new RelayCommand(this.PreviousViewModel, CanNavigate);
}
private void PreviousViewModel() {
this.CurrentPageViewModel = _navigationServices.GetPreviousView();
}
private bool CanNavigate() {
this.PreviousViewCommand.NotifyCanExecuteChanged(); /// it is here where the StackOverflow Exception is being thrown
return _navigationService.CanNavigate;
}
Navigation in WPF MVVM is a bit of a pain, but this seems like an easy piece of data binding between a button, command and the CanExecute. Can someone help me in finding what I'm missing?