Search code examples
c#wpfavalondock

AvalonDock ILayoutUpdateStrategy example needed


So far, I have mainly used Edi and AvalonDockMVVM to learn how AvalonDock works, but neither provide a detailed example of how to use ILayoutUpdateStrategy for my application.

XAML of my Window looks something liike this:

<avalonDock:DockingManager.LayoutItemTemplateSelector>
                <pane:PanesTemplateSelector>
                    <pane:PanesTemplateSelector.WorkspaceExplorerTemplate>
                        <DataTemplate>
                            <v:WorkspaceExplorerView/>
                        </DataTemplate>
                    </pane:PanesTemplateSelector.WorkspaceExplorerTemplate>
                    <pane:PanesTemplateSelector.ToolBoxTemplate>
                        <DataTemplate>
                            <TextBlock>Test</TextBlock>
                        </DataTemplate>
                    </pane:PanesTemplateSelector.ToolBoxTemplate>
                </pane:PanesTemplateSelector>
            </avalonDock:DockingManager.LayoutItemTemplateSelector>

            <avalonDock:DockingManager.LayoutUpdateStrategy>
                <pane:LayoutInitializer/>
            </avalonDock:DockingManager.LayoutUpdateStrategy>

            <avalonDock:LayoutRoot>
                <avalonDock:LayoutPanel Orientation="Horizontal">
                    <avalonDock:LayoutAnchorablePane Name="DockLeft" DockMinWidth="250"/>
                    <avalonDock:LayoutAnchorablePaneGroup>
                        <avalonDock:LayoutAnchorablePane Name="DockCenterTop" DockMinWidth="250"/>
                        <avalonDock:LayoutAnchorablePane Name="DockCenterBottom" DockMinWidth="250"/>
                    </avalonDock:LayoutAnchorablePaneGroup>
                    <avalonDock:LayoutAnchorablePane Name="DockRight" DockMinWidth="500"/>
                </avalonDock:LayoutPanel>
            </avalonDock:LayoutRoot>

I would like to specifiy the following inside LayoutInitializer:

LayoutItems with the Template "WorkspaceExplorerTemplate" should be placed inside "DockLeft" and LayoutItems with the Template "ToolBoxTemplate" should be placed inside "DockRight".

Is this possible?

How would I go about this?

I don't expect a detailed explanation, but a link to a well documtned example would be great.

Thanks in advance

So far LayoutInitializer looks like this:

class LayoutInitializer : ILayoutUpdateStrategy
    {
        public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
        {
            LayoutAnchorablePane destPane = destinationContainer as LayoutAnchorablePane;
            if (destinationContainer != null &&
                destinationContainer.FindParent<LayoutFloatingWindow>() != null)
                return false;

            var toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "ToolsPane");
            if (toolsPane != null)
            {
                toolsPane.Children.Add(anchorableToShow);
                return true;
            }

            return false;

        }


        public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown)
        {
        }


        public bool BeforeInsertDocument(LayoutRoot layout, LayoutDocument anchorableToShow, ILayoutContainer destinationContainer)
        {
            return false;
        }

        public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown)
        {

        }

Solution

  • Found an Answer:

    BeforeInsertAnchorable needs to look line this:

    public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
        {
            LayoutAnchorablePane destPane = destinationContainer as LayoutAnchorablePane;
            if (destinationContainer != null &&
                destinationContainer.FindParent<LayoutFloatingWindow>() != null)
                return false;
    
            var DockLeftPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "DockLeft");
            if (DockLeftPane != null)
            {
                if(anchorableToShow.Content.GetType() == typeof(WorkspaceExplorerViewModel))
                    DockLeftPane.Children.Add(anchorableToShow);
                return true;
            }
    
            return false;
    
        }