Still confused about using MVVM with the TabbedMdiHost

Docking/MDI for WPF Forum

Posted 11 years ago by Erin Fitzhenry
Version: 12.2.0571
Avatar

Hello,

I have looked through the samples and read through the forums, and I am still confused about how to create tabs automatically to match an ObservableCollection in my ViewModel.  It seems like doing this should be a lot simpler than what I am seeing, so I am hoping that there is something fundamental that I don't understand and that you can quickly clear up.

Here is my code:

<Window x:Class="ActiproTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:local="clr-namespace:ActiproTest"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:docking="http://schemas.actiprosoftware.com/winfx/xaml/docking" 
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MainViewModel x:Key="MainViewModelDataSource" />
        <DataTemplate x:Key="DocItemTemplate">
            <TextBlock Text="Yippee!" />
        </DataTemplate>
        <Style x:Key="DockingItemStyle" TargetType="docking:DockingWindow">
            <Setter Property="Description" Value="{Binding Text}" />
            <Setter Property="Title" Value="{Binding Title}" />
        </Style>
        <!-- docking:DocumentWindow -->
        <Style x:Key="DocumentItemStyle" TargetType="docking:DocumentWindow" BasedOn="{StaticResource DockingItemStyle}">
            <Setter Property="FileName" Value="{Binding Title}" />
            <Setter Property="IsReadOnly" Value="True" />
        </Style>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource MainViewModelDataSource}}">
        <docking:DockSite  DocumentItemsSource="{Binding DocumentViewModels}" DocumentItemTemplate="{StaticResource DocItemTemplate}" DocumentItemContainerStyle="{StaticResource DocumentItemStyle}">
            <docking:DockSite.AutoHideLeftContainers>
                <docking:ToolWindowContainer >
                    <docking:ToolWindow Title="Analyzers">
                        <TextBlock Text="The palette..." />
                    </docking:ToolWindow>
                </docking:ToolWindowContainer>
            </docking:DockSite.AutoHideLeftContainers>
            <docking:DockSite.AutoHideRightContainers>
                <docking:ToolWindowContainer>
                    <docking:ToolWindow Title="Settings">
                        <TextBlock Text="Some settings..." />
                    </docking:ToolWindow>
                </docking:ToolWindowContainer>
            </docking:DockSite.AutoHideRightContainers>
            <docking:DockSite.AutoHideBottomContainers>
                <docking:ToolWindowContainer>
                    <docking:ToolWindow Title="Log" />
                    <docking:ToolWindow Title="Errors" />
                </docking:ToolWindowContainer>
            </docking:DockSite.AutoHideBottomContainers>
            <docking:Workspace>
                <docking:TabbedMdiHost />
            </docking:Workspace>
        </docking:DockSite>
    </Grid>
</Window>

 

using System.Collections.ObjectModel;

namespace ActiproTest
{
    public class MainViewModel
    {
        private ObservableCollection<DocumentViewModel> _documentViewModels = new ObservableCollection<DocumentViewModel>(); 

        public ObservableCollection<DocumentViewModel> DocumentViewModels
        {
            get { return _documentViewModels; }
        }

        public MainViewModel()
        {
            _documentViewModels.Add(new DocumentViewModel("Document 1", "Hello, how are you?"));
            _documentViewModels.Add(new DocumentViewModel("Doc 2", "Great! Thanks :)"));
        }
    }
}

 

namespace ActiproTest
{
    public class DocumentViewModel
    {
        public string Title { get; private set; }
        public string Text { get; private set; }

        public DocumentViewModel(string title, string text)
        {
            Title = title;
            Text = text;
        }
    }
}

Thanks a lot,

-Erin

Comments (3)

Answer - Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

I think you are missing the sampleCommon:DockSiteViewModelBehavior.IsManaged="true" call that we have in our MVVM demo.  That class is in the Common folder and provides default behavior for watching for bound items and them opening them.  So if you don't have that then you probably will have a blank layout.  Keep in mind that docking windows are more complicated than regular ItemsControls because docking windows can be made opened or closed while still retaining themselves in the items source.


Actipro Software Support

Posted 11 years ago by Erin Fitzhenry
Avatar

I copied sampleCommon:DockSiteViewModelBehavior.IsManaged="true" from the sample, but then that referred to other code that I was going to need to copy, and at that point I assumed that I was doing something wrong.

Are we meant to copy the class or just add a reference to the sample solution?  Can you explain why the code is not part of the main library?  I am sure you have a good reason; it will help me to understand the library to know what it is.

Thanks.

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

You can just copy the class to your own project.  I think the general idea was that some of that functionality needs to be done for MVVM to work properly but it's also an area where customers may wish to further customize the behaviors, which is why we present it open source instead of having it built into the library.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.