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

一笑奈何 提交于 2019-12-03 09:39:07
Nick Strupat

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) {}
}
user2314510

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!