2012-08-08 64 views
0

我有一个删除按钮的DataGrid XAML的样子:为什么我的Silverlight Datagrid Delete Button不能触发事件?

<sdk:DataGridTemplateColumn Header="Del/Tgl" > 
    <sdk:DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <Button Content="Delete" 
      Command="{Binding DeleteRowCommand}" 
        CommandParameter="{Binding Column}" 
        /> 
     </DataTemplate> 
    </sdk:DataGridTemplateColumn.CellTemplate> 
</sdk:DataGridTemplateColumn> 

我实现的ICommand为DelegateCommand通过复制约翰爸爸的代码。我添加了一个公共属性,以我的视图模型:

public ICommand DeleteRowCommand {get;set;} 

在我的ViewModel的构造函数中我设置的命令:

this.DeleteRowCommand = new DelegateCommand(onDelete, CanDelete); 

,最终确定的onDelete,并且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; 
} 

一切工作在我的Silvelight网格上,但点击删除按钮,我永远不会去onDelete函数。我究竟做错了什么?

+1

基本上,命令绑定将在Object内查找DeleteRowCommand属性(我的意思是绑定为ItemSource到数据网格的对象的列表)。因此,如果您使用的是SL5,则需要设置绑定来源或使用relativesource。 – vinod8812 2012-08-08 18:13:25

+1

谢谢你是这个问题的酒。我只是将绑定更改为: Command =“{Binding Source = {StaticResource VM},Path = DeleteRowCommand}”现在正在工作。你可以添加你的评论作为答案,以便我接受答案,并给你一个观点。再次感谢 – 2012-08-08 19:29:13

回答

1

基本上,命令绑定将在对象内查找DeleteRowCommand属性(我的意思是绑定为ItemSource到数据网格的对象的列表)。因此,如果您使用的是SL5,则需要设置绑定来源或使用relativesource。

干杯! Vinod

相关问题