c# - DataGridTemplateColumn 中的命令绑定(bind)

标签 c# silverlight mvvm binding datagridtemplatecolumn

我正在开发一个广泛使用 Prism、MVVM 模式和 MEF 的 Silverlight 应用程序。出于多种原因,我选择遵循 View 优先的方法。

在其中一个 View 中有一个 DataGrid,该网格的列之一是 DataGridTemplateColumn,它只有一个 Button。

我想在按钮上同时定义命令和命令参数。 Command 应该是 ViewModel 的 DelegateCommand。 CommandParameter 应该是直接来自 dataGrid 的 SelectedItems 列表。

我已经尝试了几种方法来执行此操作,但 Command 或 CommandParameter 为空。

它遵循我最初编写的代码:

<sdk:DataGridTemplateColumn>
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Width="15" Height="15" Content=">" 
                    Command="{Binding UpdateSearchParametersCommand}" 
                    CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

有人可以建议我最好的方法是什么吗?

提前致谢, 詹卢卡。

最佳答案

您当前的绑定(bind)指向 DataGridRowItem.UpdateSearchParametersCommand。您需要将其更改为指向 DataGrid.DataContext.UpdateSearchParametersCommand

<sdk:DataGrid x:Name=dataGrid>
    <sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Width="15" Height="15" Content=">" 
                        Command="{Binding DataContext.UpdateSearchParametersCommand, ElementName=dataGrid}" 
                        CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
            </DataTemplate>
        </sdk:DataGridTemplateColumn.CellTemplate>
    </sdk:DataGridTemplateColumn>
</sdk:DataGrid>

关于c# - DataGridTemplateColumn 中的命令绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583135/

相关文章:

silverlight - 具有子窗口和共享数据的Silverlight MVVM模式

c# - 将 SWIG 与 C# 结合使用时的最佳实践是什么?

c# - 如何获取导致 MVC 中的 "A potentially dangerous Request.Form value was detected from the client"的表单值?

c# - 以编程方式绑定(bind) DataGridTemplateColumn

silverlight - 从 DataTemplate 中的 DataTemplate 中引用 ViewModel DataContext

c# - 中继命令可以执行和任务

c# - 访问cookie过期时间owin

c# - CopyFileEx 包装器参数

WPF MVVM - 属性更改触发 Storyboard 仅发生一次

silverlight - Silverlight和多个 View 模型