Question about ViewModel Management (DesignTime Vs

2019-02-09 18:45发布

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>

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}">

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

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?

标签: c# wpf xaml mvvm
3条回答
Animai°情兽
2楼-- · 2019-02-09 19:07

The built in MS stuff is not bad, but another more elegant and structurally sound alternative which I am busy incorporating in my project is: http://msdn.microsoft.com/en-us/magazine/dn169081.aspx

Basically, you use the MVVM Light toolkit with the SimpleIoc container it comes with and end up with the ability to serve up data for the following three scenarios:

Design time, Run time, and Test time.

Better still, the whole point of MVVM Light is to have your stuff be directly editable in Blend and there is a whole series of videos and blogs and sample apps describing it all. I wish I had found these earlier in my WPF explorations.

查看更多
孤傲高冷的网名
3楼-- · 2019-02-09 19:18

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}">

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-02-09 19:26

Yes I think it will interfere with your current setup

The ViewModelLocator is a static class that returns a dummy object at design time, and a static ViewModel at runtime. This means that

  • The ViewModelLocator, not your ParentViewModel, contains your TabViewModels

  • You cannot have multiple instances of the same Tab (ViewModel) open at once

  • You cannot manage Open/Closed tabs unless you reference the UserControl, which is a violation of the MVVM principle where the ViewModel doesn't know of the View

  • You can't instantiate new copies of the TabViewModel with parameterized constructors. For example, OpenTabs.Add(new CustomerViewModel(CustomerId));

Perhaps an alternative could be a Converter? One that returns a static object if in design time, or the bound object during runtime? I've never tested such a thing but in theory it should work :)

查看更多
登录 后发表回答