0

How to execute a method of WPF Control using Commands?

I've created a RelayCommand class and Commands class, in which I'm trying to pass via lambda expressions, the method of RichTextBox class to a RelayCommand's cunstructor.

Inside the lambda I'm converting an argument to a targeted RichTextBox and then call the method Clear(). But when I try to click the MenuItem which is binded to that Command it throws the RefferenceNullException, that the argument being passed to the lambda and tried to convert to RichTextBox - is null.

How to correctly do this kind of operation??

RelayCommand code:

       class RelayCommand : ICommand
       {
           private readonly Action<object> _Execute;
           private readonly Func<object, bool> _CanExecute;

           public RelayCommand(Action<object> execute, Func<object, bool> canExecute)
           {
               if (execute == null) throw new ArgumentNullException("execute");
               _Execute = execute;
               _CanExecute = canExecute;
           }

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

           public event EventHandler CanExecuteChanged
           {
               add
               {
                   if (_CanExecute != null) CommandManager.RequerySuggested += value;
               }
               remove
               {
                   if (_CanExecute != null) CommandManager.RequerySuggested -= value;
               }
           }

           public void Execute(object parameter)
           {
               _Execute(parameter);
           }
       }

Commands code:

       class Commands
       {
           private ICommand _NewFileCommand;

           public ICommand NewFileCommand
           {
               get
               {
                   if (_NewFileCommand == null)
                   {
                       _NewFileCommand = new RelayCommand(
                           argument => { (argument as RichTextBox).Document.Blocks.Clear(); },
                         //  argument =>  (argument as RichTextBox).Document != null 
                           argument => true
                       );
                   }
                   return _NewFileCommand;
               }
           }
       }

Window resources and DataContext settings inside the MainWindow.xaml

<Window.Resources>
    <local:Commands x:Key="commandsClass" />
</Window.Resources>
<Window.DataContext>
    <local:Commands />
</Window.DataContext>

MenuItem settings inside the MainWindow.xaml

<MenuItem Header="_New" Command="{Binding NewFileCommand}" />
1

1 Answer 1

1

Update your binding to send RichTextBox as command parameter to view model.

<MenuItem Header="_New" Command="{Binding NewFileCommand}" 
    CommandParameter="{Binding ElementName=nameOfYourRichTextBox}/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.