在DataTemplate的资源集视图模型查看,但随后 [英] DataTemplate in Resource sets ViewModel to View, but then

查看:71
本文介绍了在DataTemplate的资源集视图模型查看,但随后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚的datacontext的视图设置为视图模型的许多不同的方式。

I am trying to figure the many different ways of setting datacontext of a view to a viewmodel.

一,我在这一刻oggling是这样的:

One I'm oggling at this moment goes something like this:

我有我的MainWindowResource:

I have my MainWindowResource:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:vw="clr-namespace:DemoStuffPartII.View"
                xmlns:vm="clr-namespace:DemoStuffPartII.ViewModel">

<DataTemplate DataType="{x:Type vm:PersonViewModel}">
    <vw:PersonView />
</DataTemplate>

但是,这也立刻在哪里链。我知道我应该在视图中使用一个ContentControl中。但是,什么是配置的最佳方法是什么?如何去了解呢?

But that's also immediately where I strand. I know that I should use a ContentControl in the View. But what is the best way to configure it? How to go about this?

推荐答案

这是您可以启用您的应用程序MVVM导航ViewSwitching的方式。

That is the way you can enable ViewSwitching navigation in your MVVM application.

另一名失踪位是:
在视图 - >

The other missing bits are: in the view ->

<ContentControl Content="{Binding CurrentPage}" />

在视图模型 - >(伪code)

in the ViewModel -> (pseudo code)

Prop ViewModelBase CurrentPage.

请注意但是,如果所有的u想要的是一个视图模型连接到一个视图,你能把整个的DataTemplate-ContentControl中的东西完全,只是做this.DataContext =新SomeViewModel();在codebehind。

note however that if all u want is to connect a ViewModel to a View, you can just drop the entire DataTemplate-ContentControl thing altogether, and just do this.DataContext = new SomeViewModel(); in the codebehind.

我知道虚拟机连接到浏览最彻底的方法是使用ViewModelLocator格局。谷歌ViewModelLocator。

The cleanest way I know to connect VM to Views is by using the ViewModelLocator pattern. Google ViewModelLocator.

这篇关于在DataTemplate的资源集视图模型查看,但随后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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