Is there any way to create a sticky footer in WPF?

三世轮回 提交于 2021-01-28 11:23:45

问题


I would like to have a sticky footer in WPF.

This is the only question that I found on this topic: Is there any way to create a sticky footer in xaml?

But the answer creates a fixed footer, not a sticky footer:

<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>

The problem with this solution is that when I put a DataGrid in the middle (Row 1) it occupies all the remaining empty space that pushes the bottom Label away.

I would like the bottom Label to stick to the bottom of the DataGrid when the DataGrid doesn't occupy the whole height and stay on screen when DataGrid is taller than the screen.

Pseudo code:

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>

Example with numbers:

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)

This is just an example, my screen is resizable, so the solution can't depend on screen size.


回答1:


DockPanel with inner Grid produce desired layout:

<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>



来源:https://stackoverflow.com/questions/65790870/is-there-any-way-to-create-a-sticky-footer-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!