WPF Datagrid 控制 第一行和第一列之间的空白

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF Datagrid 控制 第一行和第一列之间的空白相关的知识,希望对你有一定的参考价值。

原文:WPF Datagrid 控制 第一行和第一列之间的空白

这个位置就是 这里

技术图片

我们更改 DataGridControltemplate 模板

看树形结构 里面是一个BUtton

功能是全选 能找到了 那就直接改掉就可以

 

  1. <ControlTemplate x:Key="DataGridControlTemplate1" TargetType="{x:Type DataGrid}">
  2. <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
  3. <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
  4. <ScrollViewer.Template>
  5. <ControlTemplate TargetType="{x:Type ScrollViewer}">
  6. <Grid>
  7. <Grid.ColumnDefinitions>
  8. <ColumnDefinition Width="Auto"/>
  9. <ColumnDefinition Width="*"/>
  10. <ColumnDefinition Width="Auto"/>
  11. </Grid.ColumnDefinitions>
  12. <Grid.RowDefinitions>
  13. <RowDefinition Height="Auto"/>
  14. <RowDefinition Height="*"/>
  15. <RowDefinition Height="Auto"/>
  16. </Grid.RowDefinitions>
  17. <Button Width="208" Height="125.917" Command="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}">
  18. <Button.Visibility>
  19. <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
  20. <Binding.ConverterParameter>
  21. <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
  22. </Binding.ConverterParameter>
  23. </Binding>
  24. </Button.Visibility>
  25. </Button>
  26. <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
  27. <DataGridColumnHeadersPresenter.Visibility>
  28. <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
  29. <Binding.ConverterParameter>
  30. <DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
  31. </Binding.ConverterParameter>
  32. </Binding>
  33. </DataGridColumnHeadersPresenter.Visibility>
  34. </DataGridColumnHeadersPresenter>
  35. <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1"/>
  36. <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
  37. <Grid Grid.Column="1" Grid.Row="2">
  38. <Grid.ColumnDefinitions>
  39. <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
  40. <ColumnDefinition Width="*"/>
  41. </Grid.ColumnDefinitions>
  42. <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
  43. </Grid>
  44. </Grid>
  45. </ControlTemplate>
  46. </ScrollViewer.Template>
  47. <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  48. </ScrollViewer>
  49. </Border>
  50. </ControlTemplate>

 

改这个就可以

  1. <Button Width="208" Height="125.917" Command="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}">
  2. <Button.Visibility>
  3. <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
  4. <Binding.ConverterParameter>
  5. <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
  6. </Binding.ConverterParameter>
  7. </Binding>
  8. </Button.Visibility>
  9. </Button>

技术图片

以上是关于WPF Datagrid 控制 第一行和第一列之间的空白的主要内容,如果未能解决你的问题,请参考以下文章

Wpf DataGrid 绑定Dynamic (二维数据表)

PyQt5 QTableWidget 仅显示第一行和第一列

如何用代码选中datagrid中的某一行

如何使用 XlsxWriter 冻结第一行和第一列?

如何在熊猫中设置第一列和第一行作为索引?

Excel VBA自动填充未填充第一行和第一列