0

我需要使垂直滚动条稍微低一点(更短),因为我需要一个小按钮的空间。该按钮将悬停在右上角的 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 来释放垂直滚动条上方的一些空间(我猜应该是上边距)?

4

1 回答 1

4

我会更改 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"**/>
于 2012-09-20T08:45:13.420 回答