WPF的DataGrid列头和行头相交的单元格怎么修改样式和填充文字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF的DataGrid列头和行头相交的单元格怎么修改样式和填充文字相关的知识,希望对你有一定的参考价值。

参考技术A   从可视树种能看出,那个区域只有一个Button 是用来做全选的,所以我们只有通过修改DataGrid的默认模板来实现你的效果:

  首先看下DataGrid的默认模板:

  <ControlTemplate TargetType="DataGrid">
  <Border Background="TemplateBinding Control.Background" BorderBrush="TemplateBinding Control.BorderBrush" BorderThickness="TemplateBinding Control.BorderThickness" Padding="TemplateBinding Control.Padding" SnapsToDevicePixels="True">
  <ScrollViewer Focusable="False" Name="DG_ScrollViewer">
  <ScrollViewer.Template>
  <ControlTemplate TargetType="ScrollViewer">
  <Grid>
  <Grid.ColumnDefinitions>
  <ColumnDefinition Width="Auto" />
  <ColumnDefinition Width="*" />
  <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
  <RowDefinition Height="Auto" />
  <RowDefinition Height="*" />
  <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <Button Command="ApplicationCommands.SelectAll" Focusable="False" Style="DynamicResource ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly=DataGrid" Width="Binding Path=CellsPanelHorizontalOffset, RelativeSource=RelativeSource FindAncestor, AncestorLevel=1, AncestorType=DataGrid">
  <Button.Visibility>
  <Binding Path="HeadersVisibility" RelativeSource="RelativeSource FindAncestor, AncestorLevel=1, AncestorType=DataGrid">
  <Binding.ConverterParameter>
  <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
  </Binding.ConverterParameter>
  </Binding>
  </Button.Visibility>
  </Button>
  <DataGridColumnHeadersPresenter Grid.Column="1" Name="PART_ColumnHeadersPresenter">
  <DataGridColumnHeadersPresenter.Visibility>
  <Binding Path="HeadersVisibility" RelativeSource="RelativeSource FindAncestor, AncestorLevel=1, AncestorType=DataGrid">
  <Binding.ConverterParameter>
  <DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
  </Binding.ConverterParameter>
  </Binding>
  </DataGridColumnHeadersPresenter.Visibility>
  </DataGridColumnHeadersPresenter>
  <ScrollContentPresenter CanContentScroll="TemplateBinding ScrollViewer.CanContentScroll" CanHorizontallyScroll="False" CanVerticallyScroll="False" Content="TemplateBinding ContentControl.Content" ContentStringFormat="TemplateBinding ContentControl.ContentStringFormat" ContentTemplate="TemplateBinding ContentControl.ContentTemplate" Grid.ColumnSpan="2" Grid.Row="1" Name="PART_ScrollContentPresenter" />
  <ScrollBar Grid.Column="2" Grid.Row="1" Maximum="TemplateBinding ScrollViewer.ScrollableHeight" Name="PART_VerticalScrollBar" Orientation="Vertical" Value="Binding Path=VerticalOffset, Mode=OneWay, RelativeSource=RelativeSource TemplatedParent" ViewportSize="TemplateBinding ScrollViewer.ViewportHeight" Visibility="TemplateBinding ScrollViewer.ComputedVerticalScrollBarVisibility" />
  <Grid Grid.Column="1" Grid.Row="2">
  <Grid.ColumnDefinitions>
  <ColumnDefinition Width="Binding Path=NonFrozenColumnsViewportHorizontalOffset, RelativeSource=RelativeSource FindAncestor, AncestorLevel=1, AncestorType=DataGrid" />
  <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <ScrollBar Grid.Column="1" Maximum="TemplateBinding ScrollViewer.ScrollableWidth" Name="PART_HorizontalScrollBar" Orientation="Horizontal" Value="Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource=RelativeSource TemplatedParent" ViewportSize="TemplateBinding ScrollViewer.ViewportWidth" Visibility="TemplateBinding ScrollViewer.ComputedHorizontalScrollBarVisibility" />
  </Grid>
  </Grid>
  </ControlTemplate>
  </ScrollViewer.Template>
  <ItemsPresenter SnapsToDevicePixels="TemplateBinding UIElement.SnapsToDevicePixels" />
  </ScrollViewer>
  </Border>
  </ControlTemplate>分三行三列,其中第一行第一列就是你要修改的部分,你可以放一个Border在里面,然后放置一个布局和他原先的按钮以及你要放的文字等,如下:

  <ControlTemplate x:Key="Template1" TargetType="DataGrid">
  <Border Background="TemplateBinding Control.Background" BorderBrush="TemplateBinding Control.BorderBrush" BorderThickness="TemplateBinding Control.BorderThickness" Padding="TemplateBinding Control.Padding" SnapsToDevicePixels="True">
  <ScrollViewer Focusable="False" Name="DG_ScrollViewer">
  <ScrollViewer.Template>
  <ControlTemplate TargetType="ScrollViewer">
  <Grid>
  <Grid.ColumnDefinitions>
  <ColumnDefinition Width="Auto" />
  <ColumnDefinition Width="*" />
  <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
  <RowDefinition Height="Auto" />
  <RowDefinition Height="*" />
  <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <Border Width="Binding Path=CellsPanelHorizontalOffset, RelativeSource=RelativeSource FindAncestor, AncestorLevel=1, AncestorType=DataGrid">
  <Grid>
  <Button Command="ApplicationCommands.SelectAll" Focusable="False" Style="DynamicResource ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly=DataGrid">
  <Button.Visibility>
  <Binding Path="HeadersVisibility" RelativeSource="RelativeSource FindAncestor, AncestorLevel=1, AncestorType=DataGrid">
  <Binding.ConverterParameter>
  <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
  </Binding.ConverterParameter>
  </Binding>
  </Button.Visibility>
  </Button>
  <TextBlock Text="Hello" IsHitTestVisible="False"/>
  </Grid>
  </Border>
  <DataGridColumnHeadersPresenter Grid.Column="1" Name="PART_ColumnHeadersPresenter">
  ......当然,这样后你会发现这个区域本身很小导致文字显示不全。你可以通过修改DataGridRowHeader的宽度来改善:

  <Style TargetType="DataGridRowHeader">
  <Setter Property="Width" Value="30"/>
  </Style>本回答被提问者和网友采纳

WPF 获取DataGrid 控件选中的单元格信息

获取 DataGrid 选中的单元格的信息
DataGridCellInfo cell_Info = this.studentTable.SelectedCells[0];

studentTableItem student_Info = cell_Info.Item as studentTableItem;

 

studentTableItem  为一个自定义类 单元格中的数据绑定的是这个自定义类


以上是关于WPF的DataGrid列头和行头相交的单元格怎么修改样式和填充文字的主要内容,如果未能解决你的问题,请参考以下文章

请教在QTableView怎么才能不显示行号和列头

数据库提取器----知识点

C# wpf datagrid 动态加载数据后改变单元格颜色bug

wpf datagrid 单元格如何默认单击一次点中

C# .NET中DataGridView 的所有属性?

WPF DataGrid - 以编程方式将单元格设置为编辑模式