始终显示WPF ScrollViewer垂直滚动条

时间:2015-07-20 07:34:46

标签: c# wpf xaml

我使用ScrollViewer显示一长串项目,我使用下面的xaml结构

<Grid Background="#bdbec0">
    <Grid Name="scroll_Area">
        <Grid  Background="#bdbec0">
            <Grid>                   
                <ScrollViewer Name="scrollAreaPageView" HorizontalAlignment="Center" VerticalAlignment="Center"                                     
                        HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">       
                        <StackPanel Orientation="Horizontal" >
                           //Long List of items here so either horizontal & vertical scrollbars are coming here based on content
                         </StackPanel>
                </ScrollViewer>
            </Grid>
        </Grid>
    </Grid>
</Grid>

xaml显示scrollViewer中的scrollViewer和长内容。 但是当高度更高时,垂直滚动条会出现,但仅在中心。那么是否可以将滚动条设置为右侧并将内容仅保留在中心?类似地,如果宽度更大,水平滚动条在底部的位置总是如此 附在此处的示例图片enter image description here

2 个答案:

答案 0 :(得分:2)

要使滚动条显示在窗口的一侧,您必须将Scrollviewer拉伸到窗口的边缘。滚动查看器的滚动条显示在右边缘(水平滚动条的下边缘)。

做这样的事情:

<Grid Background="#bdbec0">
    <Grid Name="scroll_Area">
        <Grid  Background="#bdbec0">
            <Grid>                   
                <ScrollViewer Name="scrollAreaPageView"                                     
                        HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">       
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" >
                           //Long List of items here so either horizontal & vertical scrollbars are coming here based on content
                         </StackPanel>
                </ScrollViewer>
            </Grid>
        </Grid>
    </Grid>
</Grid>

我已经在Scrollviewer中创建了Stackpanel中心,Scrollviewer现在将拉伸填充它的父级。

答案 1 :(得分:1)

修复滚动查看器的大小,默认情况下滚动查​​看器的属性会无限增长,如果你没有指定大小,一旦你指定了大小并且内容滚动属性为true,它就可以了 设置内容可以滚动到true。设置滚动条的VerticalAlignment和Horizo​​ntalAlignement。

<Grid Background="#bdbec0">
        <Grid Name="scroll_Area">
            <Grid  Background="#bdbec0">
                <Grid>                   
                    <ScrollViewer Name="scrollAreaPageView" HorizontalAlignment="Right" VerticalAlignment="Top"                                     
                            HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"  CanContentScroll="True">       
                            <StackPanel Orientation="Horizontal" >
                               //Long List of items here so either horizontal & vertical scrollbars are coming here based on content
                             </StackPanel>
                    </ScrollViewer>
                </Grid>
            </Grid>
        </Grid>
    </Grid>