我正在尝试更改当前的代码并将其转移到MVVM设计中。
我有一个网格控件:
<ctl:GridContainer x:Name="GridResponses" CellValueChanged="GridResponses_CellValueChanged">
.
.
.
</ctl:GridContainer>
甚至如此:
Private Sub GridResponses_CellValueChanged(sender As Object, e As CellValueEventArgs)
If e.Column.FieldName = "PRIORITY" Then
.
.
.
End If
End Sub
现在我将上面的代码移到使用MVVM Light并执行以下操作:
<ctl:GridContainer DataContext="{Binding ResponsesDataView}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellValueChanged">
<cmd:EventToCommand Command="{Binding GridCellValueChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ctl:GridContainer>
在我的ViewModel中,我将以下RelayCommand声明如下:
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)
它永远不会执行!我甚至尝试使用无参数的RelayCommand并尝试显示MessageBox并且没有事件工作。
EventToCommand在特定事件方面是否受限?我做错了吗?
答案 0 :(得分:1)
实际上问题在于绑定。您正在ResponsesDataView
中搜索命令。你需要遍历View的DataContext -
<cmd:EventToCommand Command="{Binding DataContext.GridCellValueChangedCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType=UserControl}}"/>