使用 MVVM,如何将 XamDataGrid 的 SelectedItems 作为参数传递给 ContextMenu 引发的命令?
我试图传递 XamDataGrid 上的项目,在该项目上单击鼠标右键以打开 ContextMenu,这会在我的 ViewModel 中引发一个命令。不知何故,在调试模式下无法访问命令调用的方法。
这是从视图中截取的内容,
<ig:XamDataGrid DataSource="{Binding DrdResults}" Height="700" Width="600">
<ig:XamDataGrid.ContextMenu>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self},
Path=PlacementTarget.DataContext}"
AllowDrop="True" Name="cmAudit">
<MenuItem Header="View History"
Command="{Binding ViewTradeHistory}"
CommandParameter="{Binding Path=SelectedItems}">
</MenuItem>
</ContextMenu>
</ig:XamDataGrid.ContextMenu>
<ig:XamDataGrid.FieldSettings>
<ig:FieldSettings AllowFixing="NearOrFar"
AllowEdit="False"
Width="auto" Height="auto" />
</ig:XamDataGrid.FieldSettings>
</ig:XamDataGrid>
该视图对应的 ViewModel 中的代码如下。
public WPF.ICommand ViewTradeHistory
{
get
{
if (_viewTradeHistory == null)
{
_viewTradeHistory = new DelegateCommand(
(object SelectedItems) =>
{
this.OpenTradeHistory(SelectedItems);
});
}
return _viewTradeHistory;
}
}
最后,命令调用的实际方法如下,
private void OpenTradeHistory(object records)
{
DataPresenterBase.SelectedItemHolder auditRecords
= (DataPresenterBase.SelectedItemHolder)records;
// Do something with the auditRecords now.
}
我不确定我在这里做错了什么。任何帮助将非常感激。
谢谢, 什拉万
I'm trying to pass the item on XamDataGrid on which I do a mouse right click to open a ContextMenu, which raises a Command in my ViewModel. Somehow the method that the Command calls is not reachable in debug mode.
This is the snipped from the view
<ig:XamDataGrid DataSource="{Binding DrdResults}" Height="700" Width="600">
<ig:XamDataGrid.ContextMenu>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self},
Path=PlacementTarget.DataContext}"
AllowDrop="True" Name="cmAudit">
<MenuItem Header="View History"
Command="{Binding ViewTradeHistory}"
CommandParameter="{Binding Path=SelectedItems}">
</MenuItem>
</ContextMenu>
</ig:XamDataGrid.ContextMenu>
<ig:XamDataGrid.FieldSettings>
<ig:FieldSettings AllowFixing="NearOrFar"
AllowEdit="False"
Width="auto" Height="auto" />
</ig:XamDataGrid.FieldSettings>
</ig:XamDataGrid>
My code in the corresponding ViewModel for this View is as follows.
public WPF.ICommand ViewTradeHistory
{
get
{
if (_viewTradeHistory == null)
{
_viewTradeHistory = new DelegateCommand(
(object SelectedItems) =>
{
this.OpenTradeHistory(SelectedItems);
});
}
return _viewTradeHistory;
}
}
And lastly the actual method that gets called by the Command is as below
private void OpenTradeHistory(object records)
{
DataPresenterBase.SelectedItemHolder auditRecords
= (DataPresenterBase.SelectedItemHolder)records;
// Do something with the auditRecords now.
}
I'm not sure what am I doing incorrectly here. Any help will be very much appreciated.
Thanks,
Shravan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过改进达米安的答案(这不太有效)来实现这一点。
这是我的解决方案:
首先是行为:
然后是它的用法:
当然,您的视图模型中需要有一个“SelectedDataItems”。
编辑:视图模型中的
SelectedDataItems
属性必须首先实例化为空集合,否则它将不起作用。I had that working by improving Damian answer (which was not quite working).
Here's my solution:
First the Behaviour:
And then its usage:
Of course you'll need to have a "SelectedDataItems" in your view model.
Edit: The
SelectedDataItems
property in the view model has to be instantited first as an empty collection, otherwise it won't work.对于单个项目,infragistics 足够友善地添加了一个名为“ActiveDataItem”的可绑定 DependencyProperty,它是“选定的”项目(如果有)。
它甚至可以双向工作,即您可以从 ViewModel 中重置选择。
不幸的是,据我所知,没有类似的多重选择功能。
您必须自己实现此操作,迭代选定的记录,检查它们是否是数据记录,获取记录和数据项等......
For a single item, infragistics was kind enough to add a bindable DependencyProperty called 'ActiveDataItem', which is "the" selected item, if any.
It even works two-way, i.e. you can reset the selection from within your ViewModel.
Unfortunately, AFAIK there is no similar thing for multi-selection.
You will have to implement this on your own, iterating over the selected records, check if they are datarecords, get the record and dataitem etc...
尝试将 DataGrid 的 SelectedItem 绑定到视图模型中的属性。
然后,您可以在
OpenTradeHistory()
方法中访问此属性。Try binding your DataGrid's SelectedItem to a property in your viewmodel.
You can then access this property in your
OpenTradeHistory()
method.为了绑定到选定的项目,我选择使用 System.Interactivity 创建一个行为:
在您看来,某些地方会有类似以下内容的内容(为了简洁起见,我省略了命名空间映射):
现在你的问题是上下文菜单上的命令绑定,那是别的问题......我会重新审视这个
For binding to the selected items I chose to create a behavior using System.Interactivity:
Some where in your view would have something like the following (I've ommitted the namespace mappings for brevity):
Now your problem with the command binding on a context menu thats something else... I'll revisit this