Hi,
here is my short code in MS VS 2019 C# WPF - MVVM, can someone please explain me:
1/ where is pointed my code to after "return _SaveCommand;" which is placed to "public ICommand SaveCommand" property ? It is classic MVVM.
2/ And who is caller of "CommandManager.RequerySuggested += value;" in "add event" in "public class RelayCommand : ICommand" ?? What is "value" ???
...there are some invisible parts of code added by MS VS in backgroud ... and I do not understand to it ...
thanks J.
if someone need source code: WpfApp024 relay command v konstruktoru.zip
here is my short code in MS VS 2019 C# WPF - MVVM, can someone please explain me:
1/ where is pointed my code to after "return _SaveCommand;" which is placed to "public ICommand SaveCommand" property ? It is classic MVVM.
2/ And who is caller of "CommandManager.RequerySuggested += value;" in "add event" in "public class RelayCommand : ICommand" ?? What is "value" ???
...there are some invisible parts of code added by MS VS in backgroud ... and I do not understand to it ...
thanks J.
if someone need source code: WpfApp024 relay command v konstruktoru.zip
C#:
public class VM
{
public String Name { get; set; }
// classic Button Save described in XAML as: <Button Content="Save" Command="{Binding SaveCommand}" Margin="3" />
private ICommand _SaveCommand;
public ICommand SaveCommand
{
get { return _SaveCommand; }
}
public VM()
{
_SaveCommand = new RelayCommand(SaveCommand_Execute, SaveCommand_CanExecute);
}
public void SaveCommand_Execute()
{
MessageBox.Show("Save called");
}
public bool SaveCommand_CanExecute()
{
//MessageBox.Show("Save called");
if ( string.IsNullOrEmpty(Name))
{
return false;
}
else
{
return true;
}
}
}
public class RelayCommand : ICommand
{
private Action methodToxecute;
private Func<bool> canExecuteEvaluator;
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
//MessageBox.Show(value.ToString());
}
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
{
this.methodToxecute = methodToExecute;
this.canExecuteEvaluator = canExecuteEvaluator;
}
public RelayCommand(Action methodToExecute) : this(methodToExecute, null)
{
}
public bool CanExecute(object parameter)
{
if ( this.canExecuteEvaluator == null)
{
return true;
}
else
{
bool result = this.canExecuteEvaluator.Invoke();
return result;
}
}
public void Execute(object parameter)
{
this.methodToxecute.Invoke();
}
}