user2170838
user2170838

Reputation: 47

EventToCommand never works for me

I'm attempting to change my current code and move it towards an MVVM design.

I have a grid control as such:

        <ctl:GridContainer x:Name="GridResponses" CellValueChanged="GridResponses_CellValueChanged">
            .
            .
            .
        </ctl:GridContainer>

with an even as such:

        Private Sub GridResponses_CellValueChanged(sender As Object, e As CellValueEventArgs)
            If e.Column.FieldName = "PRIORITY" Then
               .
               .
               .
            End If
        End Sub

now I'm moving the above code to using MVVM Light and doing the following:

        <ctl:GridContainer DataContext="{Binding ResponsesDataView}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="CellValueChanged">
                    <cmd:EventToCommand Command="{Binding GridCellValueChangedCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ctl:GridContainer>

and in my ViewModel I have the following RelayCommand declared as follows:

        Private m_cmdGridCellValueChanged As RelayCommand(Of CellValueEventArgs)

        .
        .
        .

        m_cmdGridCellValueChanged = New RelayCommand(Of CellValueEventArgs) _
                (Sub(e)
                     If e.Column.FieldName = "PRIORITY" Then
                         .
                         .
                         .
                     End If
                 End Sub)

and it NEVER executes! I even tried using a parameter-less RelayCommand and tried showing a MessageBox and that didn't event work.

Is the EventToCommand limited in terms of specific events? Am I doing something wrong???

Upvotes: 0

Views: 1235

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

Actually the problem is in binding. You are searching for command in ResponsesDataView. you need to traverse up for your View's DataContext -

 <cmd:EventToCommand Command="{Binding DataContext.GridCellValueChangedCommand, 
                     RelativeSource={RelativeSource FindAncestor, 
                                         AncestorType=UserControl}}"/>

Upvotes: 1

Related Questions