I'm using MVVM Pattern. Im my View, I have textboxes for Person details, which one of them is idBox. Also, the view consists of several buttons, which one of them is editModeBtn.
I want the editModeBtn to be enabled only when there is a valid int inside the idBox.
My Xaml (within view) for the editBtn looks like the following:
<Button x:Name="editModeBtn" Content="Edit" Command="{Binding ChangeToEditScreenCommand}" CommandParameter="{Binding ElementName=idBox, Path=Text}"></Button>
In the corresponding viewModel, I have the following code:
private RelayCommand<string> _changeToEditScreenCommand;
public RelayCommand<string> ChangeToEditScreenCommand
{
get
{
if (_changeToEditScreenCommand == null)
{
_changeToEditScreenCommand = new RelayCommand<string>((param) => ChangeToEditScreen(), (param) => CanEdit(param));
}
return _changeToEditScreenCommand;
}
}
Also, in the CanExecute method (CanEdit in my case), I want to check if the parameter (id) is set to a valid int and then return true. False, otherwise.
private bool CanEdit(string currentInsertedId)
{
int idValue;
bool result = Int32.TryParse(currentInsertedId, out idValue);
if (result)
{
if (idValue > 0) { return true; };
return false;
}
return false;
}
Basically, I want the canExecute method of the command to be invoked everytime something is written or removed from the idBox. Where should I put the RaiseCanExecuteChanged() of the command? If I haven't used MVVM, I could put it in the textBox textChanged event, but this is not the case here. Never used the RaiseCanExecuteChanged, so just want to ensure. Thanks!