How do I automatically load a layout into my AvalonDock instance?

后端 未结 2 830
春和景丽
春和景丽 2021-02-11 00:23

I have integrated AvalonDock 2.0 into my application. I\'ve bound the document and anchor-able sources to my view-model which are rendered with the proper user controls via

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-11 00:35

    I had to add this in the XAML...

    ...
    
    
        
    
    
    
    ...
    

    ...and this in the code-behind...

    class LayoutUpdateStrategy : ILayoutUpdateStrategy
    {
        private bool BeforeInsertContent(LayoutRoot layout, LayoutContent anchorableToShow)
        {
            var viewModel = (ViewModelBase) anchorableToShow.Content;
            var layoutContent = layout.Descendents().OfType().FirstOrDefault(x => x.ContentId == viewModel.ContentId);
            if (layoutContent == null)
                return false;
            layoutContent.Content = anchorableToShow.Content;
            // Add layoutContent to it's previous container
            var layoutContainer = layoutContent.GetType().GetProperty("PreviousContainer", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(layoutContent, null) as ILayoutContainer;
            if (layoutContainer is LayoutAnchorablePane)
                (layoutContainer as LayoutAnchorablePane).Children.Add(layoutContent as LayoutAnchorable);
            else if (layoutContainer is LayoutDocumentPane)
                (layoutContainer as LayoutDocumentPane).Children.Add(layoutContent);
            else
                throw new NotSupportedException();
            return true;
        }
        public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
        {
            return BeforeInsertContent(layout, anchorableToShow);
        }
        public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown) {}
        public bool BeforeInsertDocument(LayoutRoot layout, LayoutDocument anchorableToShow, ILayoutContainer destinationContainer)
        {
            return BeforeInsertContent(layout, anchorableToShow);
        }
        public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown) {}
    }
    

提交回复
热议问题