我在datagrid的第一列有按鈕。我正在使用MVVM並試圖將命令綁定到ViewModel中的命令,但是當我單擊每行中的按鈕時,它不起作用(它不會在ViewModel中調用Command),但如果我將該按鈕從DataGrid中移出,它將正常工作。如何從silverlight和MVVM中的datagrid中的按鈕觸發事件
如何從MVVM中的datagrid中的按鈕觸發事件?
更新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>
視圖模型的代碼是:
public ViewModelCommand ViewOrganizationCommand { get; set; }
看着你的xaml和虛擬機代碼將會很有幫助。請添加它。 – 2010-12-27 08:59:44