MVVM + 数据上下文 + 数据模板 + 混合=问题

发布于 2024-07-16 15:43:41 字数 502 浏览 4 评论 0原文

我目前正在 WPF 项目中使用 MVVM,一切都运行良好。 我有一个主视图和许多详细视图,我使用 MasterViewModel 中的 currentView 属性来管理它们。 通过使用数据模板,我将视图绑定到视图模型。 事实上,我的主视图有一个 contentcontrol,其 content 属性绑定到我的 CurrentView 属性。 当我将此 currentview 属性设置为视图模型或另一个视图模型时,它会调用相应的模板。

我的问题是,使用这个,我的详细视图没有显式的数据上下文,因为它是由我的数据模板放置的。 因此,在混合中,当我打开视图来编辑其设计时,我没有数据可以绑定到我的视图。 如果我为详细视图设置数据上下文,则在混合中我可以看到可以绑定的所有数据,但在运行时,数据模板设置的数据上下文将被详细视图中设置的数据上下文覆盖,因此在运行时我没有数据。

有谁知道我如何创建一个好的 MVVM 项目,其中包含由数据模板管理的视图以及我们可以使用 Blend 看到的数据上下文?

谢谢,

I'm currently using MVVM in a WPF project, all works very well.
I have one Master view and many Detail views that I manage using a currentView property in my MasterViewModel. By using a datatemplate, I bind a view to a viewmodel.
In fact, my master view has a contentcontrol whose content property is binded to my CurrentView property. When I set this currentview property to a viewmodel or another, it calls the corresponding template.

My problem is that using this, my detail views doesn't have explicit datacontext because it is placed by my datatemplate. So in blend, when I open my view to edit its design, I have no datas to bind to my view. If I set a datacontext to my detailview, in blend I can see all datas I can bind but in runtime, the datacontext set by datatemplate is overrided by the datacontext set in my detailview, so I have no datas during runtime.

Does anyone knows how I can create a good MVVM project, with views managed by datatemplates and with datacontext that we can see with Blend ?

Thanks,

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

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

发布评论

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

评论(2

独夜无伴 2024-07-23 15:43:41

我有一篇关于这个问题的博客文章: http://www.robfe.com/2009/08/design-time-data-in-expression-blend-3/

我的帖子主要是关于在混合中显示数据没有 必须在运行时显示甚至创建该数据。

I've got a blog post on this issue: http://www.robfe.com/2009/08/design-time-data-in-expression-blend-3/

My post is all about showing data in blend without having to have that data displayed or even created at runtime.

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