M_K
M_K

Reputation: 109

How to call a method of WPF control using Command?

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}" />

Upvotes: 0

Views: 1649

Answers (1)

rahulaga-msft
rahulaga-msft

Reputation: 4164

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

<MenuItem Header="_New" Command="{Binding NewFileCommand}" 
    CommandParameter="{Binding ElementName=nameOfYourRichTextBox}/>

Upvotes: 1

Related Questions