0

我有一个包含 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 的附加堆栈面板一起使用,我需要做些什么?

4

1 回答 1

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>
于 2013-08-06T08:46:15.033 回答