WPF GridView标头不会水平滚动

时间:2010-11-10 13:30:03

标签: wpf gridview rounded-corners scrollviewer

我有一个带有ScrollViewer的GridView,它常常与GridView的内容一起水平滚动。我使用Chris Cavanagh's rounded corners solution更改了它,其中包括在GridViewHeaderRowPresenter周围放置一个网格,并将边框添加到同一级别,然后将其用作不透明蒙版(see this question for more information)。

这似乎禁用了标题的水平滚动。如果我将浏览器窗口的大小调整为较小的强制内容的滚动条,则标题保持不变。我想,添加的xaml会以某种方式禁用滚动标题。

这就是代码目前的样子。请注意,这是一个以ScrollViewer元素为目标的样式定义,而不是直接在页面上的XAML标记中。

<ScrollViewer Focusable="false"
              HorizontalScrollBarVisibility="Hidden"
              VerticalScrollBarVisibility="Hidden"
              DockPanel.Dock="Top"
              Style="{DynamicResource HEK_ScrollViewer}">
    <Grid>
        <Border Name="Mask"
                CornerRadius="20,20,0,0"
                Background="White" />
        <GridViewHeaderRowPresenter Margin="1,0,1,1"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                    Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}"
                                    AllowsColumnReorder="{Binding TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
                                    ColumnHeaderContainerStyle="{DynamicResource HEK-GridViewColumnHeaderStyle}"
                                    ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
                                    ColumnHeaderStringFormat="{Binding TemplatedParent.View.ColumnHeaderStringFormat, RelativeSource={RelativeSource TemplatedParent}}"
                                    ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
                                    ColumnHeaderTemplateSelector="{Binding TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
                                    ColumnHeaderToolTip="{Binding TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}">
            <GridViewHeaderRowPresenter.OpacityMask>
                <VisualBrush Visual="{Binding ElementName=Mask}" />
            </GridViewHeaderRowPresenter.OpacityMask>
        </GridViewHeaderRowPresenter>
    </Grid>
</ScrollViewer>

3 个答案:

答案 0 :(得分:1)

我不确定导致这种行为的原因。我猜测ListView中有一些代码需要GridViewHeaderRowPresenter作为标题ScrollViewer的直接子代。

但是如果你将网格作为ScrollViewer的父级向上移动,它会正确滚动。

<DockPanel Margin="{TemplateBinding Padding}">
    <Grid DockPanel.Dock="Top">
        <Border Name="Mask" CornerRadius="4,4,0,0" Background="White" />
        <ScrollViewer  Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
            <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}}" ColumnHeaderContextMenu="{Binding TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplate="{Binding TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}" Columns="{Binding TemplatedParent.View.Columns, RelativeSource={RelativeSource TemplatedParent}}" Margin="2,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                <GridViewHeaderRowPresenter.OpacityMask>
                    <VisualBrush Visual="{Binding ElementName=Mask}" />
                </GridViewHeaderRowPresenter.OpacityMask>
            </GridViewHeaderRowPresenter>
        </ScrollViewer>
    </Grid>
    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" KeyboardNavigation.DirectionalNavigation="Local" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</DockPanel>

答案 1 :(得分:1)

GridViewHeaderRowPresenter需要ScrollViewer作为父级,它是硬代码。您可以更改其模板

示例:

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

答案 2 :(得分:0)

虽然是老帖子,但如果有人有同样的问题。这有点类似于上面Tedy Pranolo的解决方案。我有这个问题,网格标题不会滚动内容。仅为ScrollViewer的ControlTemplate中的GridViewHeaderRowPresenter添加ScrollViewer(对于ListView)为我解决了这个问题。这是xaml中修复了问题的部分:

app[0].styles[]

当问题出现时,这是xaml:

styleUrls[]

注意缺少的ScrollViewer包含GridViewHeaderRowPresenter。

希望这有帮助!