WPF Notepad Application [Part 4]

In this episode of creating our WPF Notepad Application, we will be creating our RelayCommand object and begin creating some of our view models. In our FileViewModel we will set up the commands for our notepads toolbar to be able and open, save, save as and create new text documents. We we also create a EditorViewModel which will handle our formatted editor and document properties.

 

YouTube

 

Code from this episode

RelayCommand.cs

    public class RelayCommand : ICommand
    {
        readonly Action _execute;
        readonly Func<bool> _canExecute;

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new NullReferenceException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        public RelayCommand(Action execute) : this(execute, null)
        {

        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(object parameter)
        {
            _execute.Invoke();
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *