如何在MVVM中的视图之间进行交互 [英] How to interact among views in MVVM

查看:116
本文介绍了如何在MVVM中的视图之间进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是WPF + MVM的新手,有一个主视图,里面有两个子视图。一个子视图有2个按钮,另一个子视图有一个标签条有两个标签。在一个子视图中单击一个按钮如何在另一个视图中移动标签?我知道这很简单,但是我浪费了很多时间来搜索它。

解决方案

我要做的是为按钮设置一些属性/视图模型中的选项卡状态为主视图,并为主视图模型提供对子视图的视图模型的引用。子视图模型可以重新公开属性,这是在子视图中绑定的数据。



对于有点更好的封装,你可以有一个小类,它在属性中保存按钮/制表符状态,并使主视图模型创建那个类的实例,并将其引用到子视图模型中。



在任何情况下,包含按钮/选项卡状态属性的类都应实现 INotifyPropertyChanged ,以便视图跟踪状态值的变化

Hi,

I am new to WPF+MVM , a have one main view and inside it i have two child views .One child view has 2 buttons and the other child view has a tab strip having two tabs.On the click of a button in one child view how to shift tabs in the other view? i know it would be simple but i have wasted a lot of time googling for it.

解决方案

What I''d do is to have some property for the button/tab state in the View Model for the main view and provide reference to the main View Model into the View Models for the child views. The child View Models can re-expose the property which is data bound in the child views.

For a bit better encapsulation, you could have a small class which holds the button/tab state in a property and have the main View Model create an instance of that class and provide the reference to that into the child View Models.

In any case, the class containing the button/tab state property should implement INotifyPropertyChanged so that the views track changes in the state value.


这篇关于如何在MVVM中的视图之间进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆