Implement click event using mvvm RRS feed

  • Question

  • Hi Experts,

    I want to implement click event using MVVM pattern. The code I have done is as follows:-

    Inside the xaml file:-


    and then added

        <i:EventTrigger EventName="Click">
            <i:InvokeCommandAction Command="{Binding ButtonClickCommand}" />

    and removed click event from button

    inside xaml.vb page constructor

    Me.DataContext = New CoverageViewModel

    inside my viewmodel page

      Public ReadOnly Property ButtonClickCommand() As ICommand
                Return New DelegateCommand(Of Object)(AddressOf ButtonClicked)
            End Get
        End Property
        Public Sub ButtonClicked(obj As Object)
            Dim class As New Myclass
            If (Globals.IsEdited = False) Then
                'My Code
            End If
        End Sub

    But it is not working, please tell the changes I should make inside my code.



    Wednesday, June 17, 2015 12:40 PM


All replies

  • Buttons have a command property, you should bind that.

    (In fact you can template a button as any control in order to give that control that ability.)

    I also recommend you use mvvm light.

    That offers relaycommand.

    I work in c#, I'm afraid.

    Delegatecommand is similar to relaycommand, which I would use.

    But here's an example:

    <Button Content="Delete" Command="{Binding DeleteAppGroup}"/>

    That binds to a public property of the viewmodel:

        public class AdminAppGroupVM : ViewModelBase
               public RelayCommand DeleteAppGroup {get;set;}

    That's wired up to a method in the constructor of that viewmodel:

    public AdminAppGroupVM()
        DeleteAppGroup = new RelayCommand<string>(ExecuteDelete);
    private void ExecuteDelete(string ignored)

    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Edited by Andy ONeill Wednesday, June 17, 2015 1:07 PM
    Wednesday, June 17, 2015 1:07 PM
  • Hi,

    Thanks for your reply do I need to create another derived class AdminAppGroup inside my ViewModel?

    Wednesday, June 17, 2015 1:13 PM
  • Nope - don't just cut and paste that code exactly. It won't work anyhow if you're using vb.

    It's out a usercontrol which maintains a grouping of applications. That's nothing to do with your coverage stuff.

    You will have a viewmodel.

    You want a public property in that which will be a relaycommand.  You also want to connect that relaycommand up to a method inside that. I think that'd be a sub and you'd probably use some addressof stuff to wire em up.

    You set the datacontext of your view to an instance of that viewmodel.

    You can then bind to  that command, whatever you call it, by binding the command property of your button.

    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Wednesday, June 17, 2015 1:23 PM
  • Thanks,

    I am new to that, can you please tell how can I connect method to a RelayCommand.

    I created a property.

    Do I need to do anything in the constructor of a viewmodel also?

    Public Property DeleteAppGroup() As RelayCommand

    • Edited by rahul_agg11 Wednesday, June 17, 2015 2:08 PM
    Wednesday, June 17, 2015 2:06 PM
  • You're not really going to be deleting an AppGroup though, are you?

    I think you lose the brackets.

    Public Property YourCommand As RelayCommand

    and I think maybe wiring it up would be:

    Me.YourCommand = New RelayCommand(AddressOf Me.SomeSub)

    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Wednesday, June 17, 2015 2:18 PM
  • Hi rahul_ahh11,

    I'm not familiar with VB,  but I find a Sample Calculator with MVVM in VB.NET. This article introduce how to  bind click event  with MVVM. Please refer to below link. Hope it helps.

    Best Regards,

    • Marked as answer by Weiwei Cai Tuesday, June 23, 2015 7:40 AM
    Thursday, June 18, 2015 6:25 AM
  • Hi rahul_agg11,

    I will mark my post as answer, if this doesn't give you any help, please feel free to let me know and follow up.

    Best Regards,
    Tuesday, June 23, 2015 7:40 AM