代码之家  ›  专栏  ›  技术社区  ›  Andrew Keith

wpf scrollviewer调整偏移量问题

  •  0
  • Andrew Keith  · 技术社区  · 16 年前

    我有以下测试样品:

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

    这就产生了:

    Scrollbar bottom missing http://img33.imageshack.us/img33/8200/scrolloffsetmissing.jpg

    我的问题是如何设置 ScrollViewer.Height 动态的同时还能看到滚动条的底部?在我的样本中, Height ScrollViewer 太长,因为 Label 在它上面…

    我不想修复 高度 滚动条 到静态值。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Jobi Joy    16 年前

    我建议将外部StackPanel移到网格中,因为StackPanel不考虑子级大小。并移除ScrollViewer.Height绑定。现在您只需要为网格创建两个行定义,并将标签放置到grid.row=0和scrollviwer-to-grid.row=1。

    代码如下。所以我的建议是,只有在必要的时候才使用StackPanel/Canvas,并且可能用于内部级别。尝试使用网格来获得非常动态的布局。

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