MVVM - 删除记录时关闭选项卡

发布于 2024-10-04 06:24:44 字数 274 浏览 3 评论 0原文

我不太确定如何以最佳 MVVM 方式执行此操作...

基本上,我的主应用程序会打开一个搜索窗口,其中显示 TabPanel 中的所有记录。然后,如果双击记录,则会打开包含该记录的新选项卡。现在,我试图保持MVVM,但我一生都无法弄清楚当一个人删除记录时如何关闭gui tabitem(如果记录消失了为什么要保持它打开)。

我现在能弄清楚如何做到这一点的唯一方法是将 TabItem 的实例作为 DeleteCommand 的参数传递,这对我来说似乎是一个很大的禁忌,但我一生都无法弄清楚如何实现这一点。

I'm not really sure how to do this in the best MVVM way...

Basically, my main app opens up a search window that shows all records in a TabPanel. Then if a record is double clicked a new tab is opened with that record. Now, I'm trying to keep things MVVM, but I can't for the life of me figure out how to close the gui tabitem when a person deletes the record (why keep it open if the record is gone).

The only way I can figure out how to do it now is to pass the instance of the TabItem as a parameter of the DeleteCommand, which to me seems like a big no-no, but I can't for the life of me figure out how to accomplish this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2024-10-11 06:24:44

如果您要在真正的 MVVM 意义上执行此操作,那么双击记录会在幕后将记录添加到记录集合中。该记录集合是控件中选项卡的数据源。只需从列表(通常是 ObservableCollection)中删除该项目就会导致 UI 更新并删除选项卡。

您当前使用哪种方法来显示选项卡?


编辑(回应评论):

这不是“真正的”MVVM。选项卡是否不仅仅是记录并不重要。您应该创建抽象这些细节的视图模型,然后将视图模型实例(RecordViewModel、ReportViewModel 等)放入可观察集合中并绑定到该集合。使用数据模板根据当前选项卡绑定到的视图模型类型为每个选项卡的内容呈现正确的视图。

If you were going to do this in the true MVVM sense, then double-clicking a record would, behind the scenes, add a record to a collection of records. That record collection is the datasource for the tabs in your control. Simply removing that item from the list (usually an ObservableCollection<T>) would result in the UI updating and the tab being removed.

Which approach are you currently using to show the tab?


Edit (in response to comment):

That is not "true" MVVM. It doesn't matter if the tabs can be more than just records. You should create View Models which abstract those details, then just put your view model instances (RecordViewModel, ReportViewModel, etc) in an observable collection and bind to that. Use datatemplates to render the correct views for each tab's content based on the type of view model the current tab is being bound to.

溺孤伤于心 2024-10-11 06:24:44

Josh Smith 写了一篇精彩的文章,描述了 MVVM 的工作原理。示例应用程序执行的操作与您想要执行的操作非常相似。

应用程序显示一个 TabControl,其中显示 1 个或多个工作区。工作区显示两种不同类型的项目。选项卡项目是可关闭的。看看吧,我相信这会解决你的问题。

采用模型-视图-视图模型设计模式的 WPF 应用

Josh Smith wrote an amazing article describing how MVVM works. The sample application does something very similar to what you want to do.

The application displays a TabControl which displays 1 or more workspaces. The workspace area displays two different kinds of items. The tab items are closable. Take a look, I'm sure this will solve your problem.

WPF Apps With The Model-View-ViewModel Design Pattern

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文