You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于.NET5的WPF项目用AvalonDock 4.60.0实现侧边栏效果求助

问题根因

你的布局结构配置错误:当前将LayoutAnchorablePane与LayoutDocumentPane平级放在LayoutPanel容器中,会被渲染为固定分割的并排面板,无法实现锚点侧边栏的停靠、自动隐藏等预期效果。


修复方案

1. 调整外层容器结构

删除外层Grid多余的列定义,让DockingManager直接占满整个窗口内容区域,侧边栏的空间划分由AvalonDock内部自行处理,无需手动拆分。

2. 修正LayoutRoot布局结构

将侧边栏对应的LayoutAnchorablePane移动到LayoutRoot的RightSide(需要左侧侧边栏则替换为LeftSide)属性下的LayoutAnchorSide容器中。


修改后完整XAML代码

<Window x:Class="TestApp.Views.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:vm="clr-namespace:TestApp.ViewModels"
        xmlns:cm="http://www.caliburnproject.org"
        mc:Ignorable="d"
        Width="800" Height="450"
        Title="MainWindow">
    <Grid>
        <DockingManager x:Name="DockManager"
                        DocumentsSource="{Binding Items}"
                        AnchorablesSource="{Binding MenuItems}">
            <DockingManager.LayoutItemTemplate>
                <DataTemplate>
                    <ContentControl cm:View.Model="{Binding Content}" IsTabStop="False" />
                </DataTemplate>
            </DockingManager.LayoutItemTemplate>
            <DockingManager.LayoutItemContainerStyle>
                <Style TargetType="{x:Type LayoutItem}">
                    <Setter Property="Title" Value="{Binding Model.DisplayName, Mode=OneWay}"/>
                    <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}"/>
                </Style>
            </DockingManager.LayoutItemContainerStyle>
            <LayoutRoot>
                <!-- 右侧侧边栏配置 -->
                <LayoutRoot.RightSide>
                    <LayoutAnchorSide>
                        <LayoutAnchorGroup>
                            <LayoutAnchorablePane DockMinWidth="300" />
                        </LayoutAnchorGroup>
                    </LayoutAnchorSide>
                </LayoutRoot.RightSide>
                <!-- 主文档区域配置 -->
                <LayoutPanel>
                    <LayoutDocumentPaneGroup>
                        <LayoutDocumentPane />
                    </LayoutDocumentPaneGroup>
                </LayoutPanel>
            </LayoutRoot>
        </DockingManager>
    </Grid>
</Window>

可选配置

  • 若需要侧边栏默认收起、鼠标悬停才滑出,给对应的LayoutAnchorable设置AutoHideWidth="300",初始状态设为AutoHidden即可
  • 若需要禁止用户拖动侧边栏到其他位置,给LayoutAnchorable添加CanFloat="False" CanDockAsTabbedDocument="False"属性即可

内容的提问来源于stack exchange,提问作者Reydan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 17:15:08