2011-12-28 70 views
5

我有一個綁定到源的列表視圖。它工作正常。我已經設置了ScrollViewer.VerticalScrollBarVisibility =「Auto」,這樣如果沒有空間來顯示項目,它應該顯示滾動條。垂直滾動條包括WPF中ListView的標題

高興這是它工作正常。它顯示垂直滾動條,但它也包含GridViewColoumn的標題。它看起來很奇怪。

那麼如何只顯示垂直滾動條的內容,而不是頭?

任何想法都會有所幫助。

+0

您的意思是說您不希望滾動條與標題行重疊? – snurre 2011-12-28 09:40:35

+0

@snurre是的,垂直滾動條包括標題行也 – Syed 2011-12-28 09:45:30

回答

4

嘗試......

 <ListView.Resources> 
      <Style TargetType="{x:Type ScrollBar}" 
        BasedOn="{StaticResource {x:Type ScrollBar}}"> 
       <Style.Triggers> 
        <Trigger Property="Name" Value="PART_VerticalScrollBar"> 
         <Setter Property="Margin" Value="0,18,0,0"/> 
        </Trigger> 
       </Style.Triggers>     
      </Style> 
     </ListView.Resources> 

編輯:

要了解如何工作首先你必須參照這個MSDN文章,讓你列表視圖的默認模板... http://www.google.co.in/url?q=http://msdn.microsoft.com/en-us/library/ms788747(v%3Dvs.85).aspx&sa=U&ei=r_L6TuXlJ8XyrQep_anODw&ved=0CBQQFjAC&sig2=HNWppacyWyhYxn2NcUSbEw&usg=AFQjCNHzlst2jA_pMTzZsUGNxtbWBqYQLQ

在這個模板中,我們有名字PART_VerticalScrollBar定義的verticle滾動條。

現在,如果您想要更改其屬性(如邊距),則必須爲滾動條設置默認目標類型樣式。在我們上面的示例中,樣式的targetType是Scrollbar類,但它沒有資源Key!這意味着ListView下的所有滾動條都將獲取樣式並將其頂部邊距設置爲18px。但是我們不希望它適用於所有滾動條,所以我添加了一個觸發器,該觸發器只將該樣式作爲名稱爲「PART_VerticalScrollBar」的滾動條。

讓我知道這是否有幫助。

+0

謝謝,它工作正常。我改變了保證金。但是,你能否解釋它是如何工作的? – Syed 2011-12-28 10:35:35

+0

看到我上面編輯的答案。 – 2011-12-28 10:49:49

3

我會通過重寫ScrollViewer的樣式來實現這一點。這樣做的好處是它可以自動處理標題大小調整。

XAML:

<Grid> 
    <Grid.Resources> 
    <local:HeightToMarginConverter x:Key="HeightToMarginConverter"/> 
    <Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type ScrollViewer}"> 
     <Setter Property="Focusable" Value="false"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ScrollViewer}"> 
        <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="Auto"/> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="Auto"/> 
         </Grid.RowDefinitions> 
<!-- Here I set Grid.ColumnSpan to 2, so it stretches over the scrollbar --> 
         <DockPanel Margin="{TemplateBinding Padding}" Grid.ColumnSpan="2"> 
          <ScrollViewer DockPanel.Dock="Top" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> 
           <GridViewHeaderRowPresenter x:Name="gridViewHeaderRowPresenter" AllowsColumnReorder="{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContainerStyle="{Binding TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderToolTip="{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderStringFormat="{Binding TemplatedParent.View.ColumnHeaderStringFormat, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}" Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplateSelector="{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}" Margin="2,0,2,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </ScrollViewer> 
          <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" KeyboardNavigation.DirectionalNavigation="Local" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </DockPanel> 
         <ScrollBar x:Name="PART_HorizontalScrollBar" Cursor="Arrow" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0.0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/> 
<!-- Here I set the vertical scrollbar's top margin by binding it to the GridViewHeaderRowPresenter's ActualHeight property and using a converter --> 
         <ScrollBar x:Name="PART_VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0.0" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" Margin="{Binding ActualHeight, ElementName=gridViewHeaderRowPresenter, Converter={StaticResource HeightToMarginConverter}}"/> 
         <DockPanel Background="{Binding Background, ElementName=PART_VerticalScrollBar}" Grid.Column="1" LastChildFill="false" Grid.Row="1"> 
          <Rectangle DockPanel.Dock="Left" Fill="White" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Width="1"/> 
          <Rectangle DockPanel.Dock="Top" Fill="White" Height="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/> 
         </DockPanel> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    </Grid.Resources> 
    <ListView> 
    <!-- Normal stuff here --> 
    </ListView> 
</Grid> 

HeightToMarginConverter.cs:

class HeightToMarginConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value == null ? new Thickness() : new Thickness(0, (double)value, 0, 0); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return null; 
    } 
}