1

我有以下测试样本:

<Window x:Class="WpfScrollTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="200" Width="200">
    <Border>
        <StackPanel>
            <Label Width="Auto" Height="Auto" Content="Text to mess up the scrollview"/>
                <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type Border}}, Path=ActualHeight}">
                    <StackPanel>
                        <Button MinWidth="100" MinHeight="100" Content="Button"/>
                        <Button MinWidth="100" MinHeight="100" Content="Button"/>
                    </StackPanel>
                </ScrollViewer>
            </StackPanel>
    </Border>
</Window>

这创造了这个:

滚动条底部缺失

我的问题是如何ScrollViewer.Height在仍然能够看到滚动条底部的同时动态设置?在我的示例中,由于上面HeightScrollViewer原因太长了..Label

我不想将 的 修复HeightScrollViewer静态值。

4

1 回答 1

3

我建议将外部 StackPanel 删除到 Grid,因为 Stackpanel 不会尊重孩子的大小。并删除 ScrollViewer.Height 绑定。现在您只需为 Grid 创建两个 RowDefinition 并将 Label 放置到 Grid.Row=0 并将 ScrollViwer 放置到 Grid.Row=1。

代码如下。所以我的建议是,仅在必要时使用 StackPanel/Canvas,并且可能用于内部级别。尝试更多地使用 Grid 以获得非常动态的布局。

  <Border>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Width="Auto" Height="Auto" Content="Text to mess up the scrollview"/>
        <ScrollViewer Grid.Row="1" >
            <StackPanel>
                <Button MinWidth="100" MinHeight="100" Content="Button"/>
                <Button MinWidth="100" MinHeight="100" Content="Button"/>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Border>
于 2009-10-19T05:19:27.820 回答