首页 > 解决方案 > 有没有办法在 WPF 中创建一个粘性页脚?

问题描述

我想在 WPF 中有一个粘性页脚。

这是我在这个主题上发现的唯一问题: 有没有办法在 xaml 中创建粘性页脚?

但是答案会创建一个固定的页脚,而不是一个粘性页脚:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="Label at the top"/>

    <DataGrid Grid.Row="1"/>

    <Label Grid.Row="2" Grid.Column="0" Content="Label at the bottom"/>
</Grid>

该解决方案的问题在于,当我将 aDataGrid放在中间(第 1 行)时,它会占据所有剩余的空白空间,从而将底部Label推开。

当底部不占据整个高度时,我希望底部Label粘在底部,并且在高于屏幕时留在屏幕上。DataGridDataGridDataGrid

伪代码:

if DataGrid needs scrollbar
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
else
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

数字示例:

DataGrid needs a scrollbar:
    screen height: 1000 px
    filled data grid height: 2500 px
    sticky footer height: 30 px
    sticky footer y from top: 970 px (screen height - sticky footer height)

DataGrid does not need a scrollbar:
    screen height: 1000 px
    empty data grid height: 100 px
    sticky footer height: 30 px
    sticky footer y from top: 100 px (same as data grid height)

这只是一个例子,我的屏幕是可调整大小的,所以解决方案不能依赖于屏幕大小。

标签: wpfxamlfootersticky

解决方案


带有内部 Grid 的 DockPanel 产生所需的布局:

<DockPanel LastChildFill="False">
    <Label Content="Label at the top" DockPanel.Dock="Top"/>

    <Grid DockPanel.Dock="Top">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <DataGrid Grid.Row="0" />

        <Label Grid.Row="1" Grid.Column="0" Content="Label at the bottom"/>
    </Grid>
</DockPanel>

长窗

短窗


推荐阅读