I am trying to bind a RelayCommand’s CanExecute in my main window to a child window that possibly does not exist. How should I do it?
Currently I have:
<MenuItem Header="_Compact"
Command="{Binding Path=CurrentChildViewModel.CompactCommand}"
IsEnabled="{Binding CurrentChildViewModel.CanExecuteCompactCommand,
Converter={StaticResource NullToBooleanConverter}}"/>
However this does not seem to work because the converter should work on CurrentChildViewModel (and not the CanExecuteCompactCommand, but I also should include that CanExecuteCompactCommand somehow.
I want the menu item to be enabled only if CurrentChildViewModel != null and CurrentChildViewModel.CanExecuteCompactCommand() returns true.
(reason: the CurrentChildViewModel is a window’s ViewModel that can be opened or not, if it is not opened, I want the menu item to be disabled. And if it is opened, I want the Compact command’s CanExecute method to check if the compact command can be executed, which is something like at least two items in the listview in the ChildView (Model) are selected.)
Can anybody help please?
if your converter need the instance of
CurrentChildViewModelthen bind to that and not the command (remove.CanExecuteCompactCommand)That said why on earth are you using one command to determine if another command should be able to execute? You should utilize the CanExecute of your command (
CompactCommand).Ok I think I understand your actual problem now.
If I’m correct then your xaml/bindings work as expected unless either
CurrentChildViewModelorCanExecuteCompactCommandis null. (assuming you remove your converter.)To solve this you can add
FallbackBalue=falseto your binding, this tells the binding to usefalsewhen it cannot find the source. And also addTargetNullValue=falsethis tells the binding to usefalsewhen the source is null (CompactCommandin this case)So it would look like:
That said I still would discourage the usage of a command to determine if another command can execute. I would do would do something like this:
e.g.