wpf - 有没有办法在 WPF 中创建粘性页脚?

标签 wpf xaml footer sticky

我想在 WPF 中添加粘性页脚。

这是我在该主题上发现的唯一问题: Is there any way to create a sticky footer in 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>

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

DataGrid不占据整个高度并保持不变时,我希望底部Label粘在DataGrid的底部当 DataGrid 高于屏幕时显示在屏幕上。

伪代码:

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)

这只是一个示例,我的屏幕可以调整大小,因此解决方案不能依赖于屏幕尺寸。

最佳答案

带有内部网格的 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>

long window

short window

关于wpf - 有没有办法在 WPF 中创建粘性页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65790870/

相关文章:

javascript - jQuery Mobile 中的中心控制组

c# - 在 Windows 8.1 中打印 WPF FixedPage 时为空引用

css-float - float 页脚始终位于底部并且可见

c# - 在 WPF 中指定自定义窗口的默认外观?

c# - 创建一个列表框,其中的项目在被选中时会展开( Accordion )

c# - 应用程序标题栏消失了 - UWP 应用程序

c# - 使用自定义依赖属性的数据绑定(bind)失败

css - Div出现在滚动条上方

c# - DataContext 绑定(bind)和刷新

c# - 如何从代码隐藏文件中的合并 ResourceDictionary 获取资源?