控制ScrollViewer的垂直滚动条高度

时间:2012-09-20 08:03:38

标签: wpf size scrollviewer vertical-scrolling

我需要让垂直滚动条稍低(更短),因为我需要一个小按钮的空间按钮将悬停在右上角的ScrollViewer上。我想避免将滚动条从ScrollViewer中分离出来,只是为了在那里添加我的按钮。

这是标准的WPF ScrollViewer

 <ScrollViewer Name="Scroller" CanContentScroll="True" VerticalScrollBarVisibility="Auto"  Height="{Binding ElementName=OuterContainer, Path=ActualHeight}" Width="{Binding ElementName=OuterContainer, Path=ActualWidth}">
            <Grid Name="Container" Margin="5,5,5,5"> 
            </Grid>
</ScrollViewer>

有没有什么方法可以设置(或C#代码)一些hack,它会释放一些空间(我猜是顶部边距)在垂直滚动条上方?

1 个答案:

答案 0 :(得分:3)

我会更改ScrollViewers样式。根据MSDN库http://msdn.microsoft.com/en-us/library/aa970847.aspx,默认样式为

<Style x:Key="LeftScrollViewer"
   TargetType="{x:Type ScrollViewer}">
  <Setter Property="OverridesDefaultStyle"
      Value="True" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ScrollViewer}">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>
          <Border Grid.Column="1"
                BorderThickness="0,1,1,1">
            <Border.BorderBrush>
              <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
            </Border.BorderBrush>
            <ScrollContentPresenter />
          </Border>
          <ScrollBar x:Name="PART_VerticalScrollBar"
                   Value="{TemplateBinding VerticalOffset}"
                   Maximum="{TemplateBinding ScrollableHeight}"
                   ViewportSize="{TemplateBinding ViewportHeight}"
                   Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
          <ScrollBar x:Name="PART_HorizontalScrollBar"
                   Orientation="Horizontal"
                   Grid.Row="1"
                   Grid.Column="1"
                   Value="{TemplateBinding HorizontalOffset}"
                   Maximum="{TemplateBinding ScrollableWidth}"
                   ViewportSize="{TemplateBinding ViewportWidth}"
                   Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

因此您可以将垂直滚动条替换为

<ScrollBar x:Name="PART_VerticalScrollBar"
                   Value="{TemplateBinding VerticalOffset}"
                   Maximum="{TemplateBinding ScrollableHeight}"
                   ViewportSize="{TemplateBinding ViewportHeight}"
                   Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                     **Margin="0,12,0,0"**/>