2011-12-28 17 views
5

ソースにバインドされたリストビューがあります。それは正常に動作しています。 ScrollViewer.VerticalScrollBarVisibility = "Auto"を設定しました。アイテムを表示するスペースがない場合、スクロールバーが表示されるはずです。垂直スクロールバーには、WPFのListViewのヘッダが含まれています

これはうまくいきます。これは垂直スクロールバーを表示していますが、GridViewColoumnのヘッダーも含めています。それは奇妙に見えている。

ヘッダーではなく、内容の垂直スクロールバーを表示するにはどうすればよいですか?

どのようなアイデアが参考になります。

+0

スクロールバーがヘッダー行に重ならないようにすることはできますか? – snurre

+0

@snurreはい、垂直スクロールバーにもヘッダー行が含まれています – Syed

答えて

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> 

EDIT:

は、これが最初にあなたは私たちが名前PART_VerticalScrollBarで定義された垂直方向のスクロールバーを持っている。このテンプレートでは、あなたにリストビューのデフォルトのテンプレートを提供し、この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

を参照する必要がありますどのように機能するかを理解するために。

ここで、Marginなどのプロパティを変更する場合は、スクロールバーのデフォルトのターゲットタイプスタイルを設定する必要があります。上記の例では、スタイルのtargetTypeはScrollbarクラスですが、リソースはありませんKey!これは、その下のすべてのスクロールバーがListViewで、スタイルを取得し、上端を18pxに設定することを意味します。しかし、私たちはすべてのスクロールバーに適用したくないので、このスタイルをスクロールバーに "PART_VerticalScrollBar"という名前でターゲットするトリガを追加しました。

これが役立つかどうか教えてください。

+0

ありがとう、それは正常に動作しています。私はマージンを変更しました。しかし、あなたはそれがどのように働いているかを説明することができますか? – Syed

+0

私の答えは上記の編集を参照してください。 –

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; 
    } 
}