有关视图模型管理问题(DesignTime VS运行时间) [英] Question about ViewModel Management (DesignTime Vs Run Time)

查看:192
本文介绍了有关视图模型管理问题(DesignTime VS运行时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很基本的WPF UI,从而用户请求会导致一个新的标签在我的TabControl打开。在的TabControl绑定到的ObservableCollection< ViewModelBase>

I have a fairly basic WPF UI whereby user requests cause a new tab to open in my TabControl. The TabControl is bound to an ObservableCollection<ViewModelBase>

我添加视图模型实例此集合,并相应的选项卡的内容显示基于模板是这样的:

I add ViewModel instances to this collection, and the corresponding tab's content is displayed based on templates like this:

    <DataTemplate DataType="{x:Type viewModels:UserUploadsViewModel}">
        <userControls:UserUploads />
    </DataTemplate>

现在让我们假设UserUploads控制,这里面我想在XAML线了一个虚拟机用设计帮助,像这样的:

Now let's say that inside of the UserUploads control I'd like to wire up a VM in XAML to help with the designing, like this:

<UserControl x:Class=".....UserUploads"
   .....
    DataContext="{Binding Source={StaticResource ViewModelLocater},
                  Path=UserAdministrationViewModel}">

这属性将在运行时返回一个VM提供现场服务和虚拟机在设计时模拟数据

This property will return a VM with live services at runtime, and a VM with mock data at design time.

问:这会不会XAML干扰了我在做什么了的TabItems内容绑定到视图模型的实例,并依托上述的DataTemplate使这种权利观?如果是这样,有没有办法让这两个概念一起工作?

Question: Will this XAML interfere with what I'm doing in binding a TabItems content to a ViewModel instance, and relying on the dataTemplate above to render the right View? If so, is there a way to get both of these concepts to work together?

推荐答案

还有就是要做到这一点更简单的方法。有DesignTimeUserAdministrationViewModel并在构造函数与静态数据填充它,然后指在用户控件为:

There is an easier way to do this. Have a DesignTimeUserAdministrationViewModel and populate it with static data in the constructor and refer that in UserControl as:

<UserControl d:DataContext="{d:DesignInstance designTimeVMs:DesignTimeUserAdministrationViewModel, IsDesignTimeCreatable=True}">



这种方式你必然会设计时的测试数据D:的DataContext 和运行时绑定到实际的实时数据的DataContext 。更多细节这里

This way you have a design time test data bound to d:DataContext and runtime live data bound to the actual DataContext. More details here.

这篇关于有关视图模型管理问题(DesignTime VS运行时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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