关于正确重新模板化DataGrid

时间:2011-05-12 10:45:47

标签: wpf templates datagrid wpfdatagrid scrollviewer

我最近有关于滚动和焦点变化的this problem

我通过添加DataGrid以下内容解决了这个问题:

            <DataGrid.Template>
                <ControlTemplate>

                    <ItemsPresenter />
                </ControlTemplate>

            </DataGrid.Template>

滚动现在可以正常工作,但是出现了一个新问题:DataGrid的标题不再显示了。这实际上是逻辑,因为现在我的DataGrid只显示ItemsPresenter,而不会处理标题。因此,我尝试定义一个合适的模板,首先显示标题,然后显示ItemsPresenter中的项目,以便我轻松滚动。 我只是遗漏了什么,或者是一个不同的属性来覆盖?

谢谢!

1 个答案:

答案 0 :(得分:5)

这是默认的DataGrid模板(在Classic主题中):

<ControlTemplate TargetType="{x:Type DataGrid}">
    <Border Background="{TemplateBinding Background}"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}"
      SnapsToDevicePixels="True"
      Padding="{TemplateBinding Padding}">
        <ScrollViewer   Focusable="false"
                Name="DG_ScrollViewer">
            <ScrollViewer.Template>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <Button Command="{x:Static DataGrid.SelectAllCommand}"
                                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=CellsPanelHorizontalOffset}"
                                Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type DataGrid}, ResourceId=DataGridSelectAllButtonStyle}}"
                                Focusable="false"
                                Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.All}}" />
                        <DataGridColumnHeadersPresenter Grid.Column="1" 
                                           Name="PART_ColumnHeadersPresenter"
                                           Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Column}}"/>

                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="1" Grid.ColumnSpan="2" CanContentScroll="{TemplateBinding CanContentScroll}" />

                        <ScrollBar Grid.Row="1" Grid.Column="2" Name="PART_VerticalScrollBar"
                                 Orientation="Vertical"
                                 Maximum="{TemplateBinding ScrollableHeight}"
                                 ViewportSize="{TemplateBinding ViewportHeight}"
                                 Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                 Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>

                        <Grid Grid.Row="2" Grid.Column="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <ScrollBar Grid.Column="1"
                                       Name="PART_HorizontalScrollBar"
                                       Orientation="Horizontal"
                                       Maximum="{TemplateBinding ScrollableWidth}"
                                       ViewportSize="{TemplateBinding ViewportWidth}"
                                       Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                       Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>

                        </Grid>
                    </Grid>
                </ControlTemplate>
            </ScrollViewer.Template>
            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </ScrollViewer>
    </Border>
</ControlTemplate>

请注意,它使用DataGridColumnHeadersPresenter。您还需要将其添加到模板中。