Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about ViewModel Management (DesignTime Vs Run Time)

Tags:

c#

mvvm

wpf

xaml

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 ViewModel 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 ViewModel with live services at runtime, and a ViewModel 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?

like image 806
Adam Rackis Avatar asked Jul 14 '11 15:07

Adam Rackis


1 Answers

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.

like image 111
anivas Avatar answered Oct 27 '22 09:10

anivas