I have datagrid with a delete button XAML looks like:
<sdk:DataGridTemplateColumn Header="Del/Tgl" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete"
Command="{Binding DeleteRowCommand}"
CommandParameter="{Binding Column}"
/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
I implemented ICommand as DelegateCommand by copying the code from John Papa. I added a public property to my ViewModel :
public ICommand DeleteRowCommand {get;set;}
In the constructor of my viewModel I set the command:
this.DeleteRowCommand = new DelegateCommand(onDelete, CanDelete);
and finally defined the onDelete, and CanDelete:
private void onDelete(object param)
{
// Get the Column Name
string strColumnName = param as string ?? string.Empty;
}
private bool CanDelete(object param)
{
// If we ae here we can delete the row
return true;
}
Everything works on my Silvelight grid but the delete button click, and I never go to onDelete function. What am I doing wrong?
Basically Command binding will look for DeleteRowCommand property inside the Object ( I mean the list of object that is binded as ItemSource to the datagrid). So you need to set the Source of Binding or use relativesource if you are using SL5.
Cheers!
Vinod