Avalondock MVVM可锚定位置



我在我的MVVM WPF应用程序中有一个Avalondock(版本3.5(。绑定如下:

    <xcad:DockingManager Name="_dockingManager" Grid.Row="1" 
DataContext="{Binding DockingManagerViewModel}"
                     DocumentsSource="{Binding Documents}"
                     AnchorablesSource="{Binding Anchorables}" > 

因此,当我在视图模型中向相应的"锚点"集合中添加可锚定的新锚定时,会显示相应的视图。尽管如此,这些视图总是显示在我应用程序的右侧。我如何通过视图模型控制视图在我应用程序的左侧停靠?

我认为您不能在ViewModel中控制此。

有2种控制此方法的方法。

  1. 您可以在以前保存的(默认(布局中还原布局 申请是第一次启动的或

  2. 您可以设置XAML以根据需要使用初始布局(首选解决方案(

对于第二个选项:您可以在Dockingmanager类中使用XAML绑定来实现您的要求:

有关以下片段的完整实现,请参见testapp示例(只需将左侧更改为底部以查看效果(:

<avalonDock:DockingManager Grid.Row="1">
...
    <avalonDock:LayoutRoot.LeftSide>
        <avalonDock:LayoutAnchorSide>
            <avalonDock:LayoutAnchorGroup>
                <avalonDock:LayoutAnchorable Title="AutoHide1 Content" ContentId="AutoHide1Content" IconSource="/AvalonDock.TestApp;component/Images/address-book--pencil.png" >
                    <TextBox Text="{Binding TestTimer, Mode=OneWay, StringFormat='AutoHide Attached to Timer ->{0}'}"/>
                </avalonDock:LayoutAnchorable>
                <avalonDock:LayoutAnchorable Title="AutoHide2 Content" ContentId="AutoHide2Content">
                    <StackPanel Orientation="Vertical">
                        <TextBox/>
                        <TextBox/>
                    </StackPanel>
                </avalonDock:LayoutAnchorable>
            </avalonDock:LayoutAnchorGroup>
        </avalonDock:LayoutAnchorSide>
    </avalonDock:LayoutRoot.LeftSide>
</avalonDock:LayoutRoot>
</avalonDock:DockingManager>

您可以将属性(称为InitialPosition或类似的东西(添加到可锚定的视图模型中,并实现ILayoutUpdateStrategy以将锚定在左,右或底部的位置。/p>

将类似的东西添加到您的XAML:

<xcad:DockingManager …>
   …
   <xcad:DockingManager.LayoutUpdateStrategy>
        <local:LayoutUpdate />
   </xcad:DockingManager.LayoutUpdateStrategy>
</xcad:DockingManager>

和您的LayoutUpdate类:

class LayoutUpdate: ILayoutUpdateStrategy
{
    static Dictionary<PaneLocation, string> _paneNames = new Dictionary<PaneLocation, string>
    {
        { PaneLocation.Left, "LeftPane" },
        { PaneLocation.Right, "RightPane" },
        { PaneLocation.Bottom, "BottomPane" },
    };
    public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
    {
        if (anchorableToShow.Content is IAnchorable anch)
        {
            var initLocation = anch.InitialLocation;
            string paneName = _paneNames[initLocation];
            var anchPane = layout.Descendents()
                           .OfType<LayoutAnchorablePane>()
                           .FirstOrDefault(d => d.Name == paneName);
            if (anchPane == null)
            {
                 anchPane = CreateAnchorablePane(layout, Orientation.Horizontal, initLocation);
            }
            anchPane.Children.Add(anchorableToShow);
            return true;
        }
        return false;
    }
    static LayoutAnchorablePane CreateAnchorablePane(LayoutRoot layout, Orientation orientation,
                PaneLocation initLocation)
    {
        var parent = layout.Descendents().OfType<LayoutPanel>().First(d => d.Orientation == orientation);
        string paneName = _paneNames[initLocation];
        var toolsPane = new LayoutAnchorablePane { Name = paneName };
        if (initLocation == PaneLocation.Left)
            parent.InsertChildAt(0, toolsPane);
        else
            parent.Children.Add(toolsPane);
        return toolsPane;
    }
    public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorable)
    {
        // here set the initial dimensions (DockWidth or DockHeight, depending on location) of your anchorable
    }

此代码是从工作应用程序中提取并更改的,具有不同的类型和名称。它可能应该有效,但是某处可能有错字或其他错误。

最新更新