[英]How do I customize the display of column headers in my WPF Datagrid?
我正在尝试自定义DataGrid。 我想在列标题上方插入一些内容。 我正在尝试使用ControlTemplate来执行此操作。 我的XAML代码如下。 我的问题是<ContentPresenter />
没有输出任何东西。 当我加载页面时, after
TextBlock直接出现before
TextBlock before
,其间没有任何内容。 我想在该空间中显示列标题。
<DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="True">
<DataGrid.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical">
<TextBlock>before</TextBlock>
<ContentPresenter /> <!-- outputs nothing -->
<TextBlock>after</TextBlock>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</DataGrid.Template>
</DataGrid>
如何before
TextBlock before
和TextBlock after
显示列标题? My List对象只是某个泛型类的BindingList,它具有一些公共属性。
我找到了答案。 我应该使用<DataGridColumnHeadersPresenter />
而不是<ContentPresenter />
。 所以我的代码看起来像:
<DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="True">
<DataGrid.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical">
<TextBlock>before</TextBlock>
<DataGridColumnHeadersPresenter />
<TextBlock>after</TextBlock>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</DataGrid.Template>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.