WPF ScrollViewer - 将水平滚动条对齐到顶部?

时间:2010-08-24 16:13:45

标签: c# wpf scrollbar scrollviewer

我想知道是否可以将ScrollViewer控件中的水平Scrollbar停靠在顶部而不是将其放在底部?如果没有,是否可以在ScrollViewer上方使用一个完整的单独Scrollbar控件,并以某种方式将ScrollBar_Scroll事件分配给它?

提前致谢!

1 个答案:

答案 0 :(得分:4)

当然 - 这是ScrollViewer的默认模板,用于转置网格行0和网格行1,将水平滚动条放在顶部:

<ControlTemplate x:Key="ScrollViewerHorizontalOnTopTemplate" TargetType="{x:Type ScrollViewer}">
            <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" MinHeight="17"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Rectangle x:Name="Corner" 
                    Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" 
                    Grid.Column="1" Grid.Row="0"/>
                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.Column="0" Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
                <ScrollBar x:Name="PART_VerticalScrollBar" 
                    Cursor="Arrow" 
                    Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
                    Grid.Column="1" Grid.Row="1" 
                    AutomationProperties.AutomationId="VerticalScrollBar" 
                    Maximum="{TemplateBinding ScrollableHeight}" 
                    Minimum="0" 
                    Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" 
                    ViewportSize="{TemplateBinding ViewportHeight}"/>
                <ScrollBar x:Name="PART_HorizontalScrollBar" 
                    Cursor="Arrow" 
                    Grid.Column="0" 
                    AutomationProperties.AutomationId="HorizontalScrollBar" 
                    Maximum="{TemplateBinding ScrollableWidth}" 
                    Minimum="0" 
                    Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" 
                    Orientation="Horizontal" 
                    ViewportSize="{TemplateBinding ViewportWidth}"/>
            </Grid>
        </ControlTemplate>

将此模板应用到ScrollViewer,您应该全部设定。

Expression Blend让这样的任务变得微不足道 - 我推荐它。