Xaml ScrollViewer滚动条未显示

时间:2013-08-06 08:10:21

标签: .net wpf xaml

我有一个包含ScrollViewer的UserControl。

<UserControl>
    <ScrollViewer Name="ScrollContainerScroller"
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Hidden">
       <ListBox Name="MyItemsListBox" ItemsSource="{Binding MyItems}"/>
    </ScrollViewer>
</UserControl>

在堆叠面板内使用此UserControl时,不会显示ScrollViewer的滚动条。

<Border>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/> <!-- top -->
            <RowDefinition Height="*"/>    <!-- content -->
            <RowDefinition Height="Auto"/> <!-- bottom -->
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="1" VerticalAlignment="Top">
            <Grid Name="ContentButtons" Height="20">
                <Button Name="MinimizeContent"/>
                <Button Name="MaximizeContent"/>
            </Grid>
            <ContentControl Name="MyContent"/>
        </StackPanel>

        <!--
        <ContentControl Name="MyContent" Grid.Row="1" VerticalAlignment="Top"/>
        -->

        <Grid Name="MyBottomControls" Grid.Row="2" VerticalAlignment="Bottom">
                <Button Name="Delete" HorizontalAlignment="Left"  Style="{StaticResource DeleteButtonStyle}"/>
                <Button Name="Close" HorizontalAlignment="Center" Style="{StaticResource CloseButtonStyle}"/>
                <Button Name="Hide" HorizontalAlignment="Right"  Style="{StaticResource HideButtonStyle}"/>
         </Grid>

        <!-- shows up at last so the Grid can place it at the very top it in the Z axis -->
        <ContentControl Name="MyTop" Grid.Row="0" VerticalAlignment="Top"/>
    </Grid>
</Border>

当我删除包装堆栈面板时,滚动查看器显示正确。

如何让滚动条也适用于包装MyContent-contentcontrol的额外堆栈窗口,我该怎么办?

1 个答案:

答案 0 :(得分:3)

StackPanel调整大小不会计算可以调用滚动条出现的最大高度或宽度。您应该使用Grid而不是对齐其中的项目。

<Border>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/> <!-- top -->
            <RowDefinition Height="*"/>    <!-- content -->
            <RowDefinition Height="Auto"/> <!-- bottom -->
        </Grid.RowDefinitions>

        <Grid Grid.Row="1" VerticalAlignment="Top">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/> 
                <RowDefinition Height="*"/>    
            </Grid.RowDefinitions>
            <Grid Name="ContentButtons" Height="20">
                <Button Name="MinimizeContent"/>
                <Button Name="MaximizeContent"/>
            </Grid>
            <ContentControl Grid.Row="1" Name="MyContent"/>
        </Grid >

        <!--
        <ContentControl Name="MyContent" Grid.Row="1" VerticalAlignment="Top"/>
        -->

        <Grid Name="MyBottomControls" Grid.Row="2" VerticalAlignment="Bottom">
                <Button Name="Delete" HorizontalAlignment="Left"  Style="{StaticResource DeleteButtonStyle}"/>
                <Button Name="Close" HorizontalAlignment="Center" Style="{StaticResource CloseButtonStyle}"/>
                <Button Name="Hide" HorizontalAlignment="Right"  Style="{StaticResource HideButtonStyle}"/>
         </Grid>

        <!-- shows up at last so the Grid can place it at the very top it in the Z axis -->
        <ContentControl Name="MyTop" Grid.Row="0" VerticalAlignment="Top"/>
    </Grid>
</Border>