我在数据网格的第一列有一个按钮。我正在使用MVVM尝试将命令绑定到ViewModel中的命令,但是当我单击每一行中的按钮时,它不起作用(它不调用ViewModel中的命令),但如果我将该按钮移出数据网格,则可以正常工作。
如何在MVVM中从数据网格内部的按钮触发事件?
更新1:
XAML代码如下:
<datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center">
<Button x:Name="button" Content="View" Margin="5" DataContext="{StaticResource XDataContext}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ViewOrganizationCommand}"
CommandParameter="{Binding ElementName=dtgOrganizations, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellTemplate>
ViewModel的代码是:
public ViewModelCommand ViewOrganizationCommand { get; set; }