Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

MVVM Light - usage of RaiseCanExecuteChanged for RelayCommand

$
0
0

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:

<Buttonx:Name="editModeBtn"Content="Edit"Command="{Binding ChangeToEditScreenCommand}"CommandParameter="{Binding ElementName=idBox, Path=Text}"></Button>

In the corresponding viewModel, I have the following code:

privateRelayCommand<string> _changeToEditScreenCommand;publicRelayCommand<string>ChangeToEditScreenCommand{get{if(_changeToEditScreenCommand ==null){
                _changeToEditScreenCommand =newRelayCommand<string>((param)=>ChangeToEditScreen(param),(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.

privateboolCanEdit(string currentInsertedId){int idValue;bool result =Int32.TryParse(currentInsertedId,out idValue);if(result){if(idValue >0){returntrue;};returnfalse;}returnfalse;}

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 the textBox textChanged event, but this is not the case here. Never used the RaiseCanExecuteChanged, so just want to ensure. Thanks!



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>