使用命令绑定将数据从视图发送到视图模型
问题: 使用命令绑定时如何将数据发送到视图模型?因此,例如,当我单击按钮时,它会发送列表的“当前选定的索引”,以便它可以对列表的该项目执行操作
更多信息: 我正在开发一个程序,其中我有一个发货列表,并且每个发货都有一个托盘列表。我想制作一个按钮,允许我将新托盘添加到当前选定的货件中。 >编辑>为了通过另一个扳手进入工作,每个托盘都有一个产品清单。所以我不仅需要知道我所在的货物是什么,而且我还需要知道我所在的货物的哪个托盘。
当我进行命令绑定时,我不知道如何将数据发送到 ViewModel。我想保留这个纯 MVVM,所以我不想让 ViewModel 检查视图中的任何内容。
~N
编辑: 11/04/09 - 我删除了有关 ViewModel 实例化的问题部分。我会在另一个问题中再次问这个问题,因为这个问题已经很好地解决了另一个问题。我对问题做了一些其他编辑,以明确我想要的方向。并更改了一些语法位,以便在只有一个问题时不再谈论两个问题。
Question:
How do send data to a view model when using command binding? So that, for example, when i click a button, it sends the "currently selected index" of a list so that it can perform an operation on that item of the list
Further Information:
I'm working on a program where i have a list of shipments, and each shipment has a list of pallets. I want to make a button that will allow me to add a new pallet to the currently selected shipment. >Edit> And to through another wrench into the works, each pallet has a list of products. so not only do i need to know what shipment i'm on, but i also need to know what pallet of what shipment I'm on.
When I do a command binding, I have no idea how to send the data to the ViewModel. I would like to keep this pure MVVM so i don't want to have the ViewModel checking the view for anything.
~N
Edits:
11/04/09 - I removed the section of the question about the instantiation of the ViewModel. I'll ask that again in a different question as this one is well on track for solving the other question. And I made a few other edits to the question to clearify in the direction i want. as well as changed some grammatical bits so that it wasn't talking about two questions when there is only one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常从视图模型中公开 CollectionView,并在显示视图中列表的 ItemsControl 上设置 IsSynchronizedWithCurrentItem 属性。然后,当执行命令时,我可以检查 CollectionView.CurrrentItem 属性以查看当前选择的内容。
编辑:这个答案解决了你的问题中的第一个问题。 您的视图不会将当前选定的项目发送到 ViewModel,ViewModel 会跟踪当前选定的项目。因此,使用这种技术,您不需要弄清楚如何发送该信息。
在您的视图模型中是这样的:
然后在您的 xaml 中是这样的:
这样您还可以跟踪从 ViewModel 中选择的 Shipments 集合并更新命令的 CanExecute 状态。
这有帮助吗?
I usually expose a CollectionView from the view model and set the IsSynchronizedWithCurrentItem property on the ItemsControl displaying the list in the view. Then when the command is executed, I can inspect the CollectionView.CurrrentItem propety to see what is currently selected.
EDIT: This answer addresses the first question in your, um, question. Rather than your view sending the currently selected item to the ViewModel, the ViewModel keeps track of the currently selected item. So using this technique you don't need to work out how to send that information.
Something like this in your view model:
And then this in your xaml:
This way you can also track the selection of the Shipments collection from your ViewModel and update the command's CanExecute state.
Does that help any?
为了跟踪当前选定的项目,我做了一些类似于 Groky 的事情,也许这个例子更有意义。
在包含列表绑定到的集合的 ViewModel 中(在本例中我使用的是 ListBox),公开与所选项目相关的属性。
希望这就是您所寻找的。
For keeping track of the currently selected item I do something similar to Groky, maybe this example make a little more sense.
In your ViewModel that contains the collection that your list is bound to (I'm using a ListBox in this example) expose a property that relates to the selected item.
Hopefully this is what your looking for.