关于正确重新模板化DataGrid [英] About re-templating correctly DataGrid

查看:54
本文介绍了关于正确重新模板化DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了有关滚动和焦点更改的问题此问题

I recently had this problem about scrolling and focus changing.

我通过在 DataGrid 中添加以下内容来解决它:

I solved it by adding in my DataGrid the following:

            <DataGrid.Template>
                <ControlTemplate>

                    <ItemsPresenter />
                </ControlTemplate>

            </DataGrid.Template>

滚动现在可以正常工作,但是出现了一个新问题: DataGrid 的标题不再显示。这实际上是逻辑,因为现在我的 DataGrid 将只显示 ItemsPresenter ,而无需处理标题。因此,我试图定义一个合适的模板,首先显示标题,然后显示 ItemsPresenter 中的项目,使我可以轻松滚动。
我只是缺少什么,还是要重写的其他属性?

The scrolling now works fine, but a new problem appeared: the DataGrid's headers do not show anymore. It is actually logic, since now my DataGrid will only show an ItemsPresenter without taking care of the headers. I am therefore trying to define an appropriate template, showing first the headers and then the items in an ItemsPresenter, allowing me to scroll easily. Am I just missing something, or is that a different property to override?

谢谢!

推荐答案

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

This is what the default DataGrid template looks like (in the Classic theme):

<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 。您也希望将其添加到模板中。

Notice that it uses a DataGridColumnHeadersPresenter. You will want to add this to your template as well.

这篇关于关于正确重新模板化DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆