简体   繁体   English

如何在WPF Datagrid中自定义列标题的显示?

[英]How do I customize the display of column headers in my WPF Datagrid?

I am trying to customize a DataGrid. 我正在尝试自定义DataGrid。 I want to insert some content above the column headers. 我想在列标题上方插入一些内容。 I am trying to use the ControlTemplate to do this. 我正在尝试使用ControlTemplate来执行此操作。 I have my XAML code below. 我的XAML代码如下。 My problem is that the <ContentPresenter /> is not outputting anything. 我的问题是<ContentPresenter />没有输出任何东西。 When I load the page, the after TextBlock appears directly below the before TextBlock with nothing in between. 当我加载页面时, after TextBlock直接出现before TextBlock before ,其间没有任何内容。 I want to display the column headers in that space. 我想在该空间中显示列标题。

<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>

How do I display the column headers between the before TextBlock and the after TextBlock? 如何before TextBlock before和TextBlock after显示列标题? My List object is simply a BindingList of some generic class that has a couple of public properties. My List对象只是某个泛型类的BindingList,它具有一些公共属性。

I found the answer. 我找到了答案。 I should use <DataGridColumnHeadersPresenter /> instead of <ContentPresenter /> . 我应该使用<DataGridColumnHeadersPresenter />而不是<ContentPresenter /> So my code that works looks like: 所以我的代码看起来像:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM