Search code examples
.netwpfmvvmdatatemplateavalondock

Using DataTemplates for View Model.


First of all i'm facing this problem when trying to upgrade Avalon Dock from 1.3 to 2.0. Anyway, I have a collection of ViewModel, which uses the data template and it turns each ViewModel into a separate tab.

So I have something like this.

 <ad:DockingManager x:Name="_dockingManager" DocumentsSource="{Binding Scenarios}">

    <ad:DockingManager.LayoutItemTemplateSelector>
        <local:PanesTemplateSelector>
            <local:PanesTemplateSelector.ScenarioMainTemplate>
                <DataTemplate >
                    <TabControl>
                        <TabItem Header="View1">
                            <winForm:WindowsFormsHost Name="_host1" />
                            <TextBlock Text="{Binding SampleText}"/>
                        </TabItem>
                        <TabItem Header="View2">
                            <winForm:WindowsFormsHost Name="_host2" />
                        </TabItem>
                    </TabControl>
                </DataTemplate>
            </local:PanesTemplateSelector.ScenarioMainTemplate>
        </local:PanesTemplateSelector>
    </ad:DockingManager.LayoutItemTemplateSelector>

    <ad:LayoutRoot>
        <ad:LayoutPanel Orientation="Vertical" >
            <ad:LayoutDocumentPane/>
        </ad:LayoutPanel>
    </ad:LayoutRoot>
</ad:DockingManager>

So what the above would do is for each of the Scenario binded to the document sources, it will create a new document in <ad:LayoutDocumentPane/>.

The binding works, except now I need to set the winFormHost's child in the ViewModel side. And I have no idea how to do that in the View Model. The binding for SampleText works though.

I just need to get the _host1 control in the view model and set it.

To me, doing this doesn't seem right/proper MVVM, cause it feels like i'm playing with the view in the view model.

Is there an alternative on doing this or is this attempt ok (if so how do i do it?)

Thanks, Kev84


Solution

  • usually viewmodels and datatemplate are connected through the datatype property

     <DataTemplate DataType="{x:Type local:MyViewmodelForHost1}">
       <winForm:WindowsFormsHost Name="_host1" />
     </DataTemplate/>
    

    i dunno how your scenario object look like, but if i assume that it has to properties Host1, Host2.

            <local:PanesTemplateSelector.ScenarioMainTemplate>
                <DataTemplate >
                    <TabControl>
                        <TabItem Header="View1">
                            <ContentPresenter Content="{Binding Host1}"/>
                            <TextBlock Text="{Binding SampleText}"/>
                        </TabItem>
                        <TabItem Header="View2">
                            <ContentPresenter Content="{Binding Host2}"/>
                        </TabItem>
                    </TabControl>
                </DataTemplate>
            </local:PanesTemplateSelector.ScenarioMainTemplate>
    

    this not worked if host1 and 2 are the same type. maybe you can add some viewmodel code to your question.