DataGridTemplateColumn 内的命令绑定
我正在开发一个 Silverlight 应用程序,该应用程序广泛使用 Prism、MVVM 模式和 MEF。出于多种原因,我选择遵循视图优先的方法。
在其中一个视图中有一个 DataGrid,该网格的列之一是 DataGridTemplateColumn,其中只有一个按钮。
我想在按钮上定义命令和命令参数。 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>
有人可以建议我最好的方法是什么吗?
提前致谢, 吉安卢卡.
I am working on a Silverlight application which makes an extensive use of Prism, the MVVM pattern and MEF. For several reasons, I have chosen to follow a View-first approach.
In one of the Views there is a DataGrid, and one of the columns of this grid is a DataGridTemplateColumn, which has just a Button.
I'd like to define both a Command and a CommandParameter on the Button. The Command should be a DelegateCommand of the ViewModel. CommandParameter should be the SelectedItems list coming straight out of the dataGrid.
I've tried several approaches to do this, but either Command or CommandParameter are null.
It follows the code that I originally wrote:
<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>
Could someone advice me on what the best way to go about it is?
Thanks in advance,
Gianluca.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你们中的许多人都试图帮助我解决这个问题。谢谢你。
不幸的是,提供的答案大多与 WPF 有关。
这是我解决问题的方法:
好的,这来自同一应用程序的另一个点,但原理是相同的。
如果在 a 内部定义了绑定,则在 Silverlight 中访问通常超出范围的其他元素(因为它们不是 DataTemplate 的一部分)的唯一方法是遍历 xaml 对象树。这就是 BindingHelper 的作用。
在这里发帖是因为我希望这些信息对其他人有用。
干杯,
吉安卢卡
Many of you tried to help me out on this. Thank you for that.
Unfortunately the provided answers were mostly relative to WPF.
Here is how I've solved the problem:
Ok, this comes from another point of the same application, but the principle is the same.
If a binding is defined inside a , the only way you have in Silverlight to reach out other elements that normally would be out-of-scope (as they are not part of the DataTemplate) is to walk through the xaml object tree. That's what the BindingHelper does.
Posting here as I hope the information will be useful to someone else.
Cheers,
Gianluca
您当前的绑定指向
DataGridRowItem.UpdateSearchParametersCommand
。您需要将其更改为指向 DataGrid.DataContext.UpdateSearchParametersCommandYour current binding is pointing to
DataGridRowItem.UpdateSearchParametersCommand
. You need to change it to point toDataGrid.DataContext.UpdateSearchParametersCommand
如果您使用 ItemsSource 绑定 DataGrid,则 Command 和 CommandParameter 绑定将与当前项关联 - 按照您编写的方式。
在这种情况下,您应该使用替代来源。命令应绑定到 DataContext.UpdateSearchParametersCommand 和 CommandParameter - 绑定到 DataContext.SelectedItems。
在您的情况下,在绑定项中找不到 UpdateSearchParametersCommand 和 SelectedItems。
已更新
请务必为祖先设置正确的类型。我已将其设置为窗口,但也许您正在使用用户控件。
If you bind your DataGrid using ItemsSource, then Command and CommandParameter binding is associated to the current item - the way you've written.
You should use alternative source in this case. Command should be binded to the DataContext.UpdateSearchParametersCommand and CommandParameter - to DataContext.SelectedItems.
In your case neither UpdateSearchParametersCommand, nor SelectedItems cannot be found in the binded item.
UPDATED
Be sure to set the right type for ancestor. I've set it to window, but maybe you are using UserControl.
在 silverlight 5 中,您可以执行此操作,
只需将 AncestorType 调整为您的顶级元素(UserControl、ChildWindow 等)。
In silverlight 5 you can do this
Just adjust AncestorType to be whatever your top level element is (UserControl, ChildWindow, etc).