Hi, ok once again . The code is below. If I exchange the ICommand and my ICommandX, the program does not work - it is if I run it and put down a text upper into text box and push the button no message box is appeared. If I put back the name ICommand, everything is working ok.
Here is complette source code:
https://drive.google.com/file/d/1EITi5SLB1WqVnLL0cSWV4F4F2vLnNBVt/view?usp=sharing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
/*
#region Assembly System.ObjectModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.ObjectModel.dll
#endregion
#nullable enable
*/
using System.ComponentModel;
using System.Windows.Markup;
using System.ComponentModel.DataAnnotations;
namespace WpfApp
{
[TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
[ValueSerializer("System.Windows.Input.CommandValueSerializer, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
public interface ICommandX
{
event EventHandler? CanExecuteChanged;
bool CanExecute(object? parameter);
void Execute(object? parameter);
}
public class VM
{
public String Name { get; set; }
private ICommandX _SaveCommand;
public ICommandX 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()
{
if ( string.IsNullOrEmpty(Name))
{
return false;
}
else
{
return true;
}
}
}
public class RelayCommand : ICommandX
{
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();
}
}
}