I'm using WinUI MVVM (as an MVVM newbie)
Here is my button in XAML
<Button Grid.Row="0" Content="Create New" Width="100" Margin="5"
Command="{x:Bind ViewModel.CreateNewCommand }"
Visibility="{x:Bind ViewModel.IsCreateNewVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
In the constructor of the ViewModel I have this connection:
CreateNewCommand = new RelayCommand(Handle_CreateNewCommand, CanExecuteCreateNew);
and here is the CanExecute method:
public bool CanExecuteCreateNew()
{
return IsCreateNewEnabled;
}
private bool _isCreateNewEnabled = false;
public bool IsCreateNewEnabled
{
get => _isCreateNewEnabled;
set
{
SetProperty(ref _isCreateNewEnabled, value);
}
}
If I assign the IsCreateNewEnabled property in the VM constructor it renders correctly, enabled or disabled.
When I click the button it fires the handler method and before a single line of code in that method is executed, it fires the canExecute method with a value of true. In the handler method I set IsCreateNewEnabled = false but that has no effect on the button and doesn't fire the CanExecute method.
Any ideas?
Thanks
Carl