MVVM:删除一个CustomerViewModel,但是如何获取其中的Customer模型呢?
我在组合框中有一个 CustomerViewModel 列表。我想要删除选定的 CustomerViewModel 以及包装在其中的 Customer 以将其从存储库中删除。
但是如何访问 CustomerViewModel 中的 Customer 模型呢?
I have a list of CustomerViewModels in a ComboBox. The selected CustomerViewModel I want to delete and also the Customer wrapped inside it to remove it from the repository.
But how can I access the Customer model inside the CustomerViewModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是一个建议,将您的 customerviewmodels 集合设为 CustomerViewModels 的 ObserableCollection。
这给你带来的是一个 CollectionChanged 事件,你可以用委托来监听集合的更改,即删除,这样你就可以相应地操作你的模型
http://msdn.microsoft.com/en-us/library/ms653375(VS.85).aspx
也许有什么喜欢
Just a suggestion, make your collection of customerviewmodels an ObserableCollection of CustomerViewModels.
what this buys you is a CollectionChanged Event that you could listen on with a delegate for changes to the collection ie deletion, so from there you could manipulate you model accordingly
http://msdn.microsoft.com/en-us/library/ms653375(VS.85).aspx
perhaps something like
您可能已经可以访问
CustomerViewModel
中的Customer
(VieModel 需要公开Customer
的属性,以便 View 可以对它们进行数据绑定;我通常通过直接公开Customer
或其副本来完成此操作)。关键是您不应该自己删除
客户
。这就是ViewModel
的用途,公开删除关联的Customer
的ICommand
。根据您使用的 MVVM 框架,查看DelegateCommand
或其他同等内容。您的
CustomerViewModel
将有一个
CommandTarget
(可能是Button
)到此命令。执行该命令时,将运行CustomerViewModel
的私有方法,您可以从那里删除Customer
,而不会将删除机制暴露给代码的其他部分。例如:You might have already access to the
Customer
insideCustomerViewModel
(the VieModel needs to expose the properties of theCustomer
so the View can databind on them; I usually do it by exposing theCustomer
or a copy of it directly).The point is that you should not delete the
Customer
yourself. That's what theViewModel
is for, to expose anICommand
that deletes the associatedCustomer
. Depending on which MVVM framework you are using, look intoDelegateCommand
or another equivalent.Your
CustomerViewModel
would have aand your View would bind a
CommandTarget
(probably aButton
) to this command. When the command is executed a private method ofCustomerViewModel
will be run, and you can delete theCustomer
from there without exposing the deletion mechanism to other parts of the code. For example: