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

实现按钮固定在ItemsControl底部或窗口底部(取较高位置)

解决WPF按钮自适应位置的布局问题

可以通过DockPanel嵌套的方式实现需求,无需手动设置MaxHeight,布局逻辑更简洁:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    
    <!-- 顶部导航区域 -->
    <StackPanel Orientation="Horizontal" Grid.Row="0">
        ...
    </StackPanel>
    
    <!-- 核心内容区域:滚动控件+固定按钮 -->
    <DockPanel Grid.Row="1" LastChildFill="True">
        <!-- 按钮固定在底部 -->
        <Button Content="Add Row" DockPanel.Dock="Bottom" VerticalAlignment="Top"/>
        <!-- 滚动控件填充剩余空间 -->
        <ScrollViewer
            HorizontalScrollBarVisibility="Hidden"
            VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding Settings}" 
                          HorizontalAlignment="Stretch" 
                          HorizontalContentAlignment="Stretch">
                ...
            </ItemsControl>
        </ScrollViewer>
    </DockPanel>
</Grid>

实现原理

  • 外层Grid保留顶部Auto行放置导航栏,第二行用*占满窗口剩余空间,确保内容区域始终适配窗口高度
  • 内层DockPanel通过DockPanel.Dock="Bottom"将按钮固定在区域底部,LastChildFill="True"让ScrollViewer自动填充剩余空间
  • 当ItemsControl内容较少时,ScrollViewer高度会自动收缩,按钮自然紧跟在ItemsControl下方
  • 当内容超出ScrollViewer可用高度时,ScrollViewer自动显示垂直滚动条,按钮始终停留在窗口底部区域,不会随内容滚动

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 14:55:13