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