Manage the order of creating the viewmodels

Topics: Questions
Jun 20, 2012 at 10:55 AM


Have an view with a Dockpanel

        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
            <!--  Rezept Box  -->
            <Views:RecipeBoxView DataContext="{Binding RecipeBox}" />
        <!--  TabSeiten  -->
        <Views:TabFolderView DataContext="{Binding TabFolder}" />

The RecipeBoxView is generated first with corrospendig viemodel. After that the TabFolderView with corrospendig viemodel is generated.

The problem is that TabfolderViewModel is interested in RecipeBoxViewModel on a property that is changed in the constructor of RecipeBoxViemodel.

The TabFolderViewModel gets no message because its not yet generated.

So is it possible to generate the TabFolderView with corrospending viewModel first

Jun 20, 2012 at 5:05 PM

This is a bit stupid because this depends on the order of which xaml is read. So what you might want to try is to define the TabFolderView above the recipebox.

Jun 21, 2012 at 7:50 AM

Not so easy. 2 Problems.

1.  In an Dockpanel it is not possible to change the order and have the same view as before.

2. In my project it is a little more complex this here was just the short description

Let's do this


        <!--  TabSeiten  -->
        <Views:TabFolderView DataContext="{Binding TabFolder}" />


        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
            <!--  Rezept Box  -->
            <Views:RecipeBoxView DataContext="{Binding RecipeBox}" />


TabFolderView load some more views with corresponding viewmodels. And the interested in Attribute is in one of these viewmodels.

So first the TabFolderView is genrated then the Recipeboxview only after that the Views in the TabFolderView is generated. And thats because of the views in TabfolderView  are based of Datatemplates. And the data is generated in the TabfolderViewModel.

But first every View is generated and then the Viewmodel.

So thats the order

1. Mainview
2. TabfolderView ( Views in here are not yet generated, no Data)
3. RecipeBoxView
4. MainViewModel
5. TabFolderViewModel (generates Data)
6. RecipeBoxViewModel (PropertyChangedEvent)
7. GeneralPageView (View in TabFolderView)
8. GeneralPageViewModel (which is interested in RecipeBoxViewModel)


Jun 21, 2012 at 9:30 AM

Maybe its even more complex. And i think i have to explain it again in an other way.

1. ActiveComponent Property is Changed in [ViewModelA] 
2. [ViewModelD] is  intersted in [ViewModelA] Property ActiveComponent -> Change Property ActiveRecipe
3. [ViewModelB] is also intersted in [ViewModelA] Property ActiveComponent ->  Generate [ViewModel C]
4. [ViewModelC] is interested in [ViewModelD] Property ActiveRecipe. The P

The Problem is that [ViewModelC] is generated after PropertyChangedEvent of ActiveRecipe is send.

So i have to change the order of 2 and 3.  [ViewModelB] is more intersted in [ViewModelA] then [ViewModelD] :-)

How can i change the "intersted in" oder?

Jun 21, 2012 at 8:57 PM

Please give me some more time to think about this.

Jun 22, 2012 at 6:37 AM
Edited Jun 22, 2012 at 6:45 AM

Ok. However I found an solution for me. [ViewModelC] addidional ask for the value ActiveRecipe of [ViewModelD] in the constructor.

Anyway maybe you found a solution for the "intersted in" oder.