One more question to: Multiple models in one viewmodel

Topics: Questions
Jun 26, 2012 at 10:31 AM
Edited Jun 26, 2012 at 10:31 AM

Hello i have also an question to Multiple models in one viewmodel

In my ViewModel I have 3 different Models. So I have 3 Propertys decorated with the Model Attribute.

In my view I have a TabControl: The Tabs are generated by DataTemplates.

The Tabs are UserControls of Catel and the instances of the viewmodels are generated automaticaly.

        <TabControl.Resources>
 
            <!--  Übersicht  -->
            <DataTemplate DataType="{x:Type ??? }">
                <Views:PageGeneralView />
            </DataTemplate>

...

So now the problem is what's the Datatype when I have 3 different models.

Jun 27, 2012 at 5:56 PM

The DataType is always "ViewModel". What you must do is write a custom DataTemplateSelector.

Jun 28, 2012 at 9:05 AM

DataType is "ViewModel" ??  I thought i have to override the Method

protected override System.Type GetViewModelType()

and the return value is the ViewModelType.

And in the Datatype i have to give the "Model"?

Jun 28, 2012 at 9:09 AM

That is for the whole view. But you want to create a different viw model per tab, right?

In that case, bind to a collection of models for your tab items source.

Jun 28, 2012 at 10:47 AM

Yes I wan't create a viewmodel per tabPage. And every viewmodel has more then one model.

I don't want to make a collection of models.

The ViewModel gets its Data itself from the EntityFramework.

So at the moment I have a base class PageModel. With only one Property: Name.

Then I have derived 3 classes from this base class. They implement nothing more.

 PageGeneralModel : PageModel
PageDetailModel : PageModel
PageProfileModel : PageModel
 <DataTemplate DataType="{x:Type PageGeneralModel }">
                <Views:PageGeneralView />
            </DataTemplate>

But i think the TemplateSelector is the better solution.  Let's take a look at it.
So i need only the base class. Correct?

 

Jun 28, 2012 at 1:55 PM

Yes, you need to use the template selector to return the right data template. It is easier to code.

Jun 29, 2012 at 7:24 AM
Edited Jun 29, 2012 at 7:49 AM

It is easier to code. Realy?

I don't think so. At the moment this is my code

    public class PageGeneralModel : PageModel { }
    public class PageDetailModel : PageModel { }
    public class PageProfileModel : PageModel { }
    public class PageLocationModel : PageModel { }

with a Datatemplateselector i have two write lots of more code

       public override DataTemplate
            SelectTemplate(object item, DependencyObject container)
         {
             var element = container as FrameworkElement;
 
             if (element != null && item != null && item is PageModel)
             {
                 var page = item as PageModel;
                 switch (page.Tag)
                 {
                     case PageModel.PageType.General:
                         return element.FindResource("GeneralTemplate"as DataTemplate;
                     case PageModel.PageType.Detail:
                         return element.FindResource("DetailTemplate"as DataTemplate;
                     case PageModel.PageType.Profile:
                         return element.FindResource("ProfileTemplate"as DataTemplate;
                     case PageModel.PageType.Location:
                         return element.FindResource("LocationTemplate"as DataTemplate;
                     default:
                         return null;
                 }
             }
             return null;
         }

And in the xaml it is not  less

instead of

<DataTemplate DataType="{x:Type Models:PageGeneralModel}">
    <Views:PageGeneralView />
 </DataTemplate>

i have to implement that

 <DataTemplate x:Key="GeneralTemplate">
  <Views:PageGeneralView />
 </DataTemplate>
Jun 29, 2012 at 7:51 AM

Sorry, I didn't meant "easier", but "more control", so you can "easily" handle custom logic.