MVVM 中将 RelayCommand 的处理定向到视图模型父级的正确方法

发布于 2024-08-01 21:50:11 字数 221 浏览 4 评论 0原文

我对 MVVM 甚至 WPF 都非常陌生,所以请耐心等待...

我有一个 MVVM 应用程序,它有一个主窗口,其中包含根据应用程序状态不同类型的视图模型实例。 这些视图模型之一是选项屏幕,其中包含一个用于重新启动应用程序并以其他用户身份登录数据库的按钮。 使用 RelayCommand,我如何让父窗口(主窗口)处理此命令并发出 Window.Close() 方法调用?

I'm very new to MVVM and even WPF to some degree so bear with me...

I've got a MVVM application that has a main window, containing a viewmodel instance of different types depending on application state. One of these viewmodels is an options screen which contains a button to restart the application and log into the database as a different user. Using RelayCommand, how can I have the parent, (the main window) handle this command and issue a Window.Close() method call?

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

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

发布评论

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

评论(1

影子是时光的心 2024-08-08 21:50:11

在父视图模型上定义 RelayCommand。 关联的处理程序也将在父视图模型中定义。

现在,当您创建子视图模块时,将 Paremtn View 模型对象传递给构造函数中的 Child ViewModel(依赖注入模式)。

现在您可以为视图或视图模型设置命令绑定。

另一种选择是实际使用路由命令而不是中继命令,并让它冒泡到您的父视图模型。

Define the RelayCommand on the parent view model. The Associated handlers will also be defined in the parent view model.

Now, when you creat the child view module pass the Paremtn View model object in to the Child ViewModel in the constructor (Dependency Injection Pattern).

Now you can set up the command binding for your view or view model.

Te other alternative would be to actually use a routed command instead of the relay command and let it bubble up to you parentview model.

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