【问题标题】:Why isn't my Silverlight Datagrid Delete Button firing the event?为什么我的 Silverlight Datagrid 删除按钮没有触发事件?
【发布时间】:2012-08-08 17:52:37
【问题描述】:

我有一个带有删除按钮的数据网格 XAML 看起来像:

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

我通过复制 John Papa 的代码将 ICommand 实现为 DelegateCommand。我在 ViewModel 中添加了一个公共属性:

public ICommand DeleteRowCommand {get;set;}

在我的 viewModel 的构造函数中,我设置了命令:

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

最后定义了onDeleteCanDelete

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 函数。我做错了什么?

【问题讨论】:

  • 基本上命令绑定将在对象内查找 DeleteRowCommand 属性(我的意思是作为 ItemSource 绑定到数据网格的对象列表)。因此,如果您使用的是 SL5,则需要设置绑定的来源或使用 relativesource。
  • 谢谢vino,这就是问题所在。我刚刚将绑定更改为: Command="{Binding Source={StaticResource VM}, Path= DeleteRowCommand}" 现在正在工作。您能否将您的评论添加为答案,以便我接受答案并给您一个积分。再次感谢

标签: silverlight mvvm datagrid


【解决方案1】:

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

干杯! 维诺德

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多