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

后端 未结 2 831
春和景丽
春和景丽 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...

    ...
    </ad:DockingManager.Theme>
    <ad:DockingManager.LayoutUpdateStrategy>
        <views:LayoutUpdateStrategy/>
    </ad:DockingManager.LayoutUpdateStrategy>
    
    <ad:DockingManager.LayoutItemTemplateSelector>
    ...
    

    ...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<LayoutContent>().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) {}
    }
    
    0 讨论(0)
  • 2021-02-11 00:59

    I used this :

    XAML:

    <avalonDock:DockingManager 
    x:Name="dockManager" 
    AllowMixedOrientation="True" 
    DocumentClosing="dockManager_DocumentClosing" 
    AnchorablesSource="{Binding ListUserPanelAnchorable}"
    DocumentsSource="{Binding ListUserPanel}"
    Theme="{Binding avalondockTheme}">
    

    Model :

    public ObservableCollection<UserPanel> _ListUserPanelAnchorable;
    ReadOnlyObservableCollection<UserPanel> _readonyFiles = null;
    public ReadOnlyObservableCollection<UserPanel> ListUserPanelAnchorable
    {
        get
        {
            if (_readonyFiles == null)
                _readonyFiles = new ReadOnlyObservableCollection<UserPanel>(_ListUserPanelAnchorable);
    
            return _readonyFiles;
        }
    }
    

    xaml.cs:

    private void loadLayout()
    {
        //dockPanelModel.ListUserPanel.Clear();
        //dockPanelModel.ListUserPanelAnchorable.Clear();
        var serializer = new XmlLayoutSerializer(dockManager);
        serializer = new XmlLayoutSerializer(dockManager);
        serializer.LayoutSerializationCallback += (s, args) =>
        {
            args.Content = userPanel;
                    dockPanelModel._ListUserPanelAnchorable.Add(userPanel);
        }
    }
    
    public AvaladonDockPanel()
    {
        InitializeComponent();
        this.Loaded += AvaladonDockPanel_Loaded;                
    }       
    
    void AvaladonDockPanel_Loaded(object sender, RoutedEventArgs e)
    {
        loadLayout();          
    }
    

    userpanel:

    public class UserPanel
    {
        public string Title { get; set; }
        public string ToolTip { get; set; }
        public string IconSource { get; set; }
    
        private Guid _contentGuid = Guid.NewGuid();
        public Guid ContentId
        {
            get { return _contentGuid; }
            set { _contentGuid = value; }
        }
    
    
        private UserControl _UserInterface;
        public UserControl UserInterface { get { return _UserInterface; } set { _UserInterface = value; NotifyPropertyChanged("UserInterface"); } }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                Debug.WriteLine(string.Format("PropertyChanged-> {0}", propertyName));
            }
        }
    }
    

    binding avalon to userpanel:

    <avalonDock:DockingManager.LayoutItemContainerStyle>
        <Style TargetType="{x:Type avalonDock:LayoutItem}">
            <Setter Property="Title" Value="{Binding Model.Title}"/>
            <Setter Property="ToolTip" Value="{Binding Model.ToolTip}"/>
            <Setter Property="IconSource" Value="{Binding Model.IconSource}" />
            <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
        </Style>
    </avalonDock:DockingManager.LayoutItemContainerStyle>
    <avalonDock:DockingManager.LayoutItemTemplate >
        <DataTemplate >
            <ContentPresenter Content="{Binding UserInterface}"  />
        </DataTemplate>
    </avalonDock:DockingManager.LayoutItemTemplate>
    
    0 讨论(0)
提交回复
热议问题