61

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.

Mark
  • 983
  • 2
  • 10
  • 20
  • What is "Command" in the code behind? And what is the command suppose to achieve? Selecting a Row? – jsmith Aug 20 '10 at 14:26
  • In this case "Command" isn't in the code behind. I tried to get this down to the minimum needed to explain so in this case it's just a method of the class that I'm binding to the datagrid (so it would be myObject.Command();). – Mark Aug 20 '10 at 15:44
  • As for what it should achieve, right now just for testing it just updates another property in the object which fires a propertychanged event. – Mark Aug 20 '10 at 15:45
  • I created a library that will allow you to use the following syntax `{BindTo SaveObject}`. You can find it here: http://www.simplygoodcode.com/2012/08/simpler-wpf-binding.html – Luis Perez Aug 06 '12 at 17:46
  • It amazes me that this is what is necessary to respond to a button click in MVVM. Did they ever give a reason why it's not like any other binding? – Zimano Apr 15 '20 at 14:51

5 Answers5

103

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
}
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 39
    That's considerably more complicated than I would have thought. How would I wire up that to the command in the object? Does that class need to be exposed by the bound object?> – Mark Aug 20 '10 at 15:07
  • 3
    +1 However, maybe this needs some more explanation on how to use. – HCL Aug 20 '10 at 15:31
  • 2
    HCL: Agreed, how does one wire this into the view? How exactly does this get used in the object I'm binding? It seems like maybe this is the way to go but I'm at a loss as to how I would implement this for my current needs. Thanks! – Mark Aug 20 '10 at 15:42
  • 1
    Sorry for that, I updated the post to hopefully be a little more clear on what bits of code go where. Let me know if you have questions about it. – Rachel Aug 20 '10 at 16:06
  • Thank you Rachel. I got it working! I'm going to have to spend some more time reading up on this pattern it seems! – Mark Aug 20 '10 at 16:32
  • What's the purpose of adding then removing the delegate from RequerySuggested back to back like that? – Sinaesthetic Nov 02 '13 at 05:19
  • 5
    @Sinaesthetic That's an event definition. You have `add` and `remove` for events much like you have `get` and `set` for properties. It simply tells it what to do when you add or remove a method to the event. – Rachel Nov 02 '13 at 17:52
  • What if it's a slider and I need it's double value (e)? Is it possible that way with the relayCommand? – Guy Ben-Moshe Jan 07 '16 at 15:08
  • 1
    @GuyBen-Moshe Use the `CommandParameter` property to pass anything you want to the Command, such as the slider's value. Alternatively, often the bound property is on the same object as the Command itself, so you can just access it directly. – Rachel Jan 07 '16 at 16:37
  • @GuyBen-Moshe Better to use `Path=SelectedIndex` for a slider. From there, use `public int SelectedIndex { get { return (int)GetValue(SelectedIndexProperty);} set { SetValue(SelectedIndexProperty, value);}`. Then you can set the `SelectedIndexProperty` of an object. Good example of what I think you're looking for at http://www.codeproject.com/Articles/224230/Exploring-the-use-of-Dependency-Properties-in-User – vapcguy Sep 19 '16 at 22:19
  • 2
    Just as a note: Here are comments about disadvantages of this code. But I can't say how large is the impact for typical usage. https://stackoverflow.com/questions/2281566/is-josh-smiths-implementation-of-the-relaycommand-flawed – Beauty Jun 27 '17 at 09:19
33

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.

HCL
  • 36,053
  • 27
  • 163
  • 213
  • For the Icommand idea would you implement it similarly to Rachel's idea below? – Mark Aug 20 '10 at 15:01
  • 1
    Rachel shows you an infrastructure to solve such questions generalized. There are other ways to do it but I would find it worth the time to try understand and implement it the way she showed you. – HCL Aug 20 '10 at 15:29
  • 1
    Isn't it "Command", not "Click", if you use an ICommand-based property? – EboMike Oct 24 '14 at 01:17
  • I tried the "ugly" way to see if I could use another class outside of my parent's, since I've got a UserControl and was curious if I could do something like `((UserControl1)fe.DataContext).myStackPanel.Visibility = Visibility.Hidden;`. It compiled, but it didn't work. When I clicked the button, it failed on that line saying `Object reference not set to an instance of an object`. So it must not actually be getting the reference. – vapcguy Sep 19 '16 at 22:30
9

Here is the VB.Net rendition of Rachel's answer above.

Obviously the XAML binding is the same...

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

Your Custom Class would look like this...

''' <summary>
''' Retrieves an new or existing RelayCommand.
''' </summary>
''' <returns>[RelayCommand]</returns>
Public ReadOnly Property SaveCommand() As ICommand
    Get
        If _saveCommand Is Nothing Then
            _saveCommand = New RelayCommand(Function(param) SaveObject(), Function(param) CanSave())
        End If
        Return _saveCommand
    End Get
End Property
Private _saveCommand As ICommand

''' <summary>
''' Returns Boolean flag indicating if command can be executed.
''' </summary>
''' <returns>[Boolean]</returns>
Private Function CanSave() As Boolean
    ' Verify command can be executed here.
    Return True
End Function

''' <summary>
''' Code to be run when the command is executed.
''' </summary>
''' <remarks>Converted to a Function in VB.net to avoid the "Expression does not produce a value" error.</remarks>
''' <returns>[Nothing]</returns>
Private Function SaveObject()
    ' Save command execution logic.
   Return Nothing
End Function

And finally the RelayCommand class is as follows...

Public Class RelayCommand : Implements ICommand

ReadOnly _execute As Action(Of Object)
ReadOnly _canExecute As Predicate(Of Object)
Private Event ICommand_CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

''' <summary>
''' Creates a new command that can always execute.
''' </summary>
''' <param name="execute">The execution logic.</param>
Public Sub New(execute As Action(Of Object))
    Me.New(execute, Nothing)
End Sub

''' <summary>
''' Creates a new command.
''' </summary>
''' <param name="execute">The execution logic.</param>
''' <param name="canExecute">The execution status logic.</param>
Public Sub New(execute As Action(Of Object), canExecute As Predicate(Of Object))
    If execute Is Nothing Then
        Throw New ArgumentNullException("execute")
    End If
    _execute = execute
    _canExecute = canExecute
End Sub

<DebuggerStepThrough>
Public Function CanExecute(parameters As Object) As Boolean Implements ICommand.CanExecute
    Return If(_canExecute Is Nothing, True, _canExecute(parameters))
End Function

Public Custom Event CanExecuteChanged As EventHandler
    AddHandler(ByVal value As EventHandler)
        AddHandler CommandManager.RequerySuggested, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler CommandManager.RequerySuggested, value
    End RemoveHandler
    RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
        If (_canExecute IsNot Nothing) Then
            _canExecute.Invoke(sender)
        End If
    End RaiseEvent
End Event

Public Sub Execute(parameters As Object) Implements ICommand.Execute
    _execute(parameters)
End Sub

End Class

Hope that helps any VB.Net developers!

Fütemire
  • 1,705
  • 1
  • 26
  • 21
3

Click is an event. In your code behind, you need to have a corresponding event handler to whatever you have in the XAML. In this case, you would need to have the following:

private void Command(object sender, RoutedEventArgs e)
{

}

Commands are different. If you need to wire up a command, you'd use the Commmand property of the button and you would either use some pre-built Commands or wire up your own via the CommandManager class (I think).

mdm20
  • 4,475
  • 2
  • 22
  • 24
  • 7
    Thanks yeah, that's what I meant by the last bit of my post, I got it working via a normal event handler on the code behind but I wanted a better solution. – Mark Aug 20 '10 at 15:22
0

On Xamarin Forms, the ugliest and most straightforward version:

Xaml:

<Button Margin="0,10,0,0" 
                    Text="Access galery"
                    Clicked="OpenGalery"
                    BackgroundColor="{StaticResource Primary}"
                    TextColor="White" />

then: in .cs

private async void OpenGalery(object sender, EventArgs e) 
{
//do your bidding
}
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265