Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Button click to a method

Tags:

wpf

I have a datagrid bound to an observable collection of objects. What I want to do is have a button that will execute a method of the object representing the row of the button that was clicked. So what I have now is something like this:

            <DataGridTemplateColumn Header="Command">                 <DataGridTemplateColumn.CellTemplate>                     <DataTemplate>                         <Button Name="cmdCommand" Click="{Binding Command}"                                  Content="Command"/>                     </DataTemplate>                 </DataGridTemplateColumn.CellTemplate>             </DataGridTemplateColumn> 

Which doesn't work and reports the following error:

Click="{Binding Command}" is not valid. '{Binding Command}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid.

I've looked at command binding but that looks like it would just end up going to a single external command instead of to the object bound to the row. I have it working using an event handler on the code behind and then routing it to the item bound to the selected row (since the row gets selected when the button is clicked) but that seems like poor way of handing this and I assume I'm just missing something here.

like image 480
Mark Avatar asked Aug 20 '10 14:08

Mark


2 Answers

I do this all the time. Here's a look at an example and how you would implement it.

Change your XAML to use the Command property of the button instead of the Click event. I am using the name SaveCommand since it is easier to follow then something named Command.

<Button Command="{Binding Path=SaveCommand}" /> 

Your CustomClass that the Button is bound to now needs to have a property called SaveCommand of type ICommand. It needs to point to the method on the CustomClass that you want to run when the command is executed.

public MyCustomClass {     private ICommand _saveCommand;      public ICommand SaveCommand     {         get         {             if (_saveCommand == null)             {                 _saveCommand = new RelayCommand(                     param => this.SaveObject(),                      param => this.CanSave()                 );             }             return _saveCommand;         }     }      private bool CanSave()     {         // Verify command can be executed here     }      private void SaveObject()     {         // Save command execution logic     } } 

The above code uses a RelayCommand which accepts two parameters: the method to execute, and a true/false value of if the command can execute or not. The RelayCommand class is a separate .cs file with the code shown below. I got it from Josh Smith :)

/// <summary> /// A command whose sole purpose is to  /// relay its functionality to other /// objects by invoking delegates. The /// default return value for the CanExecute /// method is 'true'. /// </summary> public class RelayCommand : ICommand {     #region Fields      readonly Action<object> _execute;     readonly Predicate<object> _canExecute;              #endregion // Fields      #region Constructors      /// <summary>     /// Creates a new command that can always execute.     /// </summary>     /// <param name="execute">The execution logic.</param>     public RelayCommand(Action<object> execute)         : this(execute, null)     {     }      /// <summary>     /// Creates a new command.     /// </summary>     /// <param name="execute">The execution logic.</param>     /// <param name="canExecute">The execution status logic.</param>     public RelayCommand(Action<object> execute, Predicate<object> canExecute)     {         if (execute == null)             throw new ArgumentNullException("execute");          _execute = execute;         _canExecute = canExecute;                }      #endregion // Constructors      #region ICommand Members      [DebuggerStepThrough]     public bool CanExecute(object parameters)     {         return _canExecute == null ? true : _canExecute(parameters);     }      public event EventHandler CanExecuteChanged     {         add { CommandManager.RequerySuggested += value; }         remove { CommandManager.RequerySuggested -= value; }     }      public void Execute(object parameters)     {         _execute(parameters);     }      #endregion // ICommand Members } 
like image 194
Rachel Avatar answered Sep 22 '22 11:09

Rachel


You have various possibilies. The most simple and the most ugly is:

XAML

<Button Name="cmdCommand" Click="Button_Clicked" Content="Command"/>  

Code Behind

private void Button_Clicked(object sender, RoutedEventArgs e) {      FrameworkElement fe=sender as FrameworkElement;     ((YourClass)fe.DataContext).DoYourCommand();      }  

Another solution (better) is to provide a ICommand-property on your YourClass. This command will have already a reference to your YourClass-object and therefore can execute an action on this class.

XAML

<Button Name="cmdCommand" Command="{Binding YourICommandReturningProperty}" Content="Command"/> 

Because during writing this answer, a lot of other answers were posted, I stop writing more. If you are interested in one of the ways I showed or if you think I have made a mistake, make a comment.

like image 25
HCL Avatar answered Sep 23 '22 11:09

HCL