BeforeInsertAnchorable永远不会调用(Avalon Dock 3.2.0.0)

时间:2018-04-22 15:22:14

标签: wpf mvvm avalondock

我试图将锚点添加到特定的定义中。为此,我尝试实现ILayoutUpdateStrategy:

public class DockingContentInitializer : ILayoutUpdateStrategy
{
    public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown)
    {

    }

    public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown)
    {

    }

    public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
    {
        LayoutAnchorablePane _Destination = destinationContainer as LayoutAnchorablePane;
        if (destinationContainer != null &&destinationContainer.FindParent<LayoutFloatingWindow>() != null)
            return false;
        if(anchorableToShow.Content is IMainAnchorContent)
        { 
            if(layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "MainPane") == null)
            {
                LayoutAnchorablePane MainPane = new LayoutAnchorablePane();
                MainPane.Name = "MainPane";
                layout.RootPanel.Children.Insert(0, MainPane);
                MainPane.Children.Add(anchorableToShow);
                return true;
            }
            else
            {
                (layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "MainPane") as LayoutAnchorablePane).Children.Add(anchorableToShow);
                return true;
            }

        }
        if (anchorableToShow.Content is IAdditionalAnchorContent)
        {
            if (layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "AdditionPane") == null)
            {
                LayoutAnchorablePane Pane = new LayoutAnchorablePane();
                Pane.Name = "AdditionPane";
                layout.RootPanel.Children.Add(Pane);
                Pane.Children.Add(anchorableToShow);
                return true;
            }
            else
            {
                (layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "AdditionPane") as LayoutAnchorablePane).Children.Add(anchorableToShow);
                return true;
            }

        }
        return false;
    }

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

MainView.xaml

 <avalon:DockingManager x:Name="DockManager" 
                           Grid.Row="2" 
                           BorderBrush="Black"
                           Margin="3"
                           Background="#FFFDFDFD"
                           BorderThickness="0"
                           DataContext="{Binding DockingManager}"
                           DocumentsSource="{Binding Documents}"
                           AnchorablesSource="{Binding Anchorables}">

        <avalon:DockingManager.Resources>
            <DataTemplate DataType="{x:Type vm:ProjectTreeViewModel}">
                <local:ProjectTreeView  />
            </DataTemplate>
            <DataTemplate DataType="{x:Type vm:ComponentTreeViewModel}">
                <local:ComponentTreeView />
            </DataTemplate>
        </avalon:DockingManager.Resources>`<avalon:DockingManager.LayoutUpdateStrategy>
            <behaviors:DockingContentInitializer />
        </avalon:DockingManager.LayoutUpdateStrategy>
        <avalon:DockingManager.LayoutItemContainerStyle>
            <Style TargetType="{x:Type avalonctrl:LayoutItem}">
                <Setter Property="Title" Value="{Binding Model.Title}"/>
                <Setter Property="CloseCommand" Value="{Binding Model.CloseTabCommand}"/>
                <Setter Property="CanClose" Value="{Binding Model.CanCloseTab}"/>
            </Style>
        </avalon:DockingManager.LayoutItemContainerStyle>
    </avalon:DockingManager>`

我的视图模型正确添加,但是从未调用过BeforeInsertAnchorable方法,这导致视图模型在右侧放置到默认的AnchorablePane。 我哪里错了?

1 个答案:

答案 0 :(得分:0)

好吧,我弄清楚了 - 我使用MVVM Light在使用DockingManager加载视图之前使用anchorables集合初始化了viewModel。这导致在RootPanel中创建两个布局而不是一个,看起来像是导致问题。

现在,我在DockingManager“Loaded”之前等待,然后才将元素放入我的Anchorable集合中。