4

So The Question has been asked before but not answered or not answered the way I would like

I know how to create the Layout I want to achive, using LayoutAnchorablePaneGroup, LayoutAnchorablePane and LayoutDocument in XAML, but I wanted to use Avalondock in a MVVM way, reducing my XAML to:

<avalonDock:DockingManager x:Name="dockingManager" 
                                       DataContext="{Binding DockManagerViewModel}"
                                       DocumentsSource="{Binding Documents}"
                                       AnchorablesSource="{Binding Anchorables}"
                                       Loaded="dockingManager_Loaded" 
                                       Unloaded="dockingManager_Unloaded">

Filling the Documents and Anchorables makes the desired windows appear in the dockingManager, but I don't see how I can secify the location in which they will appear.

How can I specify some rules ( prefferably in XAML ), to build a specific Layout, without loosing the MVVM separation?

E.G.: objects of Type A should all go togehter in a LayoutAnchorablePane on the right, Objects of type B all go together in a LayoutAnchorablePane on the left etc..

Thanks in advance.

Community
  • 1
  • 1
Philipp
  • 499
  • 5
  • 17
  • This is quite a broad question and a number of answers except for those you quoted, like http://stackoverflow.com/a/32567244/2224701, or http://www.codeproject.com/Articles/239342/AvalonDock-and-MVVM – Vojtěch Dohnal Feb 08 '16 at 13:17
  • Yes, I have seen those. Link 1 is very useful to learn the general Avalondock MVVM approach, but it doesen't answer my question ( or I don't understand the answer), while I am not shure if link 2 is still relevant. Since it doesen't use `DataContext`, `DocumentSource` or `AnchorablesSoruce`, it might be a crutch to make the older Avalondock versions MVVM-compatible. I was hoping there might be a much easier way using AvalonDock 2.0. – Philipp Feb 08 '16 at 13:27
  • So you have followed that CodePlex AvalonDock MVVM sample app approach and you have encountered some specific problem with Anchorables? `but I don't see how I can secify the place in which they will appear` - what exactly a `place` means ? You need to achieve some specific initial layout? Consider making your question a bit more specific. – Vojtěch Dohnal Feb 08 '16 at 14:40

1 Answers1

2

I went to the same situation. and found a solution which is tricky but works for me.

Followed the Solution on Code Project and implimented save and load the layout.

note that for the first time when the application starts it does not have the layout so you need to create a XML with your desired layout and later on you can load the saved layout. hope this helps.

Example Docking Manager :

  <xcad:DockingManager x:Name="DockingManagerDockView"
                         AnchorablesSource="{Binding AnchorableSource}" 
                         DocumentsSource="{Binding DocumentSource}" 
                         Utility:AvalonDockLayoutSerializer.SaveLayoutCommand="{Binding SaveLayoutCommandOnExit}"
                         Utility:AvalonDockLayoutSerializer.LoadLayoutCommand="{Binding LoadLayoutCommand}">       
    <xcad:DockingManager.Theme>
        <xcad:MetroTheme />
    </xcad:DockingManager.Theme>
    <xcad:DockingManager.LayoutUpdateStrategy>
        <Pane:LayoutInitializer/>
    </xcad:DockingManager.LayoutUpdateStrategy>
    <xcad:DockingManager.Resources>            
        <DataTemplate DataType="{x:Type ViewModels:ExplorerViewModel}">
            <Views:ExplorerView />
        </DataTemplate>            
        <DataTemplate DataType="{x:Type ViewModels:TableOfContentViewModel}">
            <Views:TableOfContentView x:Name="TOCView" Focusable="True">
                <Views:TableOfContentView.InputBindings>
                    <KeyBinding Key="F5" Command="{Binding GridF5Command}"/>
                </Views:TableOfContentView.InputBindings>
            </Views:TableOfContentView>
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModels:PropertyViewModel}">
            <Views:PropertyView />
        </DataTemplate>           
        <DataTemplate DataType="{x:Type ViewModels:SearchViewModel}">
            <Views:SearchPanel />
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModels:DocumentViewModel}">
            <Views:DocumentView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModels:ReIndexPanelViewModel}">
            <Views:ReIndexPanel />
        </DataTemplate>
    </xcad:DockingManager.Resources>       
    <xcad:DockingManager.LayoutItemContainerStyleSelector>
        <Pane:PanesStyleSelector>
            <Pane:PanesStyleSelector.ToolStyle>
                <Style TargetType="{x:Type xcad:LayoutAnchorableItem}">
                    <Setter Property="Title" Value="{Binding Model.Title}"/>
                    <Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter={x:Static Visibility.Hidden}}"/>
                    <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
                    <Setter Property="FlowDirection" Value="LeftToRight"/>
                    <Setter Property="UseLayoutRounding" Value="False"/>
                    <Setter Property="IconSource" Value="{Binding Model.IconSource}"/>
                </Style>
            </Pane:PanesStyleSelector.ToolStyle>
            <Pane:PanesStyleSelector.FileStyle>
                <Style TargetType="{x:Type xcad:LayoutItem}">
                    <Setter Property="Title" Value="{Binding Model.Title}"/>
                    <Setter Property="ToolTip" Value="{Binding Model.FilePath}"/>
                    <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
                    <Setter Property="CanClose" Value="False"/>                        
                </Style>
            </Pane:PanesStyleSelector.FileStyle>
        </Pane:PanesStyleSelector>
    </xcad:DockingManager.LayoutItemContainerStyleSelector>
    <xcad:LayoutRoot>
        <xcad:LayoutPanel Orientation="Horizontal">                
                <xcad:LayoutAnchorablePaneGroup>
                    <xcad:LayoutAnchorablePane Name="Explorer" DockMinWidth="250"/> 
                    <xcad:LayoutAnchorablePane Name="TOC" DockMinWidth="500"/>
                    <xcad:LayoutAnchorablePane Name="Property" DockMinWidth="300" />
                    <xcad:LayoutAnchorablePane Name="Search" DockMinWidth="300" />
                    <xcad:LayoutAnchorablePane Name="ReIndex" DockMinHeight="300" />
                </xcad:LayoutAnchorablePaneGroup>
            <xcad:LayoutDocumentPaneGroup >
                <xcad:LayoutDocumentPane/>
            </xcad:LayoutDocumentPaneGroup>
        </xcad:LayoutPanel>            
    </xcad:LayoutRoot>
</xcad:DockingManager>
Abin
  • 2,868
  • 1
  • 23
  • 59