简体   繁体   English

将值从一个视图模型传递到子视图模型

[英]Passing value from one viewmodel to sub viewmodel

I'm using .Net FW 4.5, VS 2013 and SQLITE. 我正在使用.Net FW 4.5,VS 2013和SQLITE。 I've got a "MainWindow" that contains tab items. 我有一个包含选项卡项目的“ MainWindow”。 Each tab item has it's own view and each view has its own VM. 每个选项卡项目都有自己的视图,每个视图都有自己的VM。 I am running a query on my MainWindow that returns data to a datagrid. 我在MainWindow上运行查询,该查询将数据返回到datagrid。 Depending on which "SelectedItem" is selected I need my query to return relevant sub data to my tab which contains my view/viewmodel... how do I get my main VM to pass a value to my Sub-VM... I've tried "FindVisualParent" but it does not work. 根据选择的是哪个“ SelectedItem”,我需要查询以将相关子数据返回到包含我的视图/视图模型的选项卡中……如何获取主虚拟机以将值传递给子虚拟机...我我已经尝试过“ FindVisualParent”,但是它不起作用。 I think it's loading up my value after everything is loaded and therefore not applying my filer on my sub-VM query... Hope this makes sense. 我认为这是在加载完所有内容之后加载我的值,因此不要在我的子VM查询中应用我的文件管理器...希望这有意义。

Best methods applicable to MVVM would be appreciated. 适用于MVVM的最佳方法将不胜感激。 Download Sample Code 下载示例代码

My best guess without seeing any code is that your views own (instantiate) their respective view models. 我没有看到任何代码的最佳猜测是您的视图拥有(实例化)它们各自的视图模型。 That would explain why you are using FindVisualParent to get at the other view models. 那可以解释为什么您使用FindVisualParent获取其他视图模型。 Try having your main view model instantiate your child view models. 尝试让主视图模型实例化子视图模型。

As mentioned, your view's View Model should instance each of the child view models and hold references to them as properties. 如前所述,您视图的视图模型应实例化每个子视图模型,并将对它们的引用作为属性保存。 Then your Tab's data context can be assigned to the view model property in the main windows view model. 然后,可以将Tab的数据上下文分配给主窗口视图模型中的视图模型属性。

This lets your parent view model register for change notification's in your child view models and gives you more control over how they interact. 这使您的父视图模型可以在子视图模型中注册更改通知,并可以更好地控制它们之间的交互方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM