关于正确重新模板化 DataGrid

发布于 2024-11-06 12:17:12 字数 644 浏览 8 评论 0原文

我最近遇到了关于滚动和焦点更改的这个问题

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

            <DataGrid.Template>
                <ControlTemplate>

                    <ItemsPresenter />
                </ControlTemplate>

            </DataGrid.Template>

现在滚动工作正常,但出现了一个新问题:DataGrid 的标题不再显示。这实际上是逻辑,因为现在我的 DataGrid 将仅显示 ItemsPresenter 而不处理标题。因此,我尝试定义一个适当的模板,首先显示标题,然后显示 ItemsPresenter 中的项目,以便我可以轻松滚动。 我只是错过了一些东西,还是需要覆盖不同的属性?

谢谢!

I recently had this problem about scrolling and focus changing.

I solved it by adding in my DataGrid the following:

            <DataGrid.Template>
                <ControlTemplate>

                    <ItemsPresenter />
                </ControlTemplate>

            </DataGrid.Template>

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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-11-13 12:17:12

默认 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。您还需要将其添加到您的模板中。

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>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文