سؤال

I am trying to customize a DataGrid. I want to insert some content above the column headers. I am trying to use the ControlTemplate to do this. I have my XAML code below. My problem is that the <ContentPresenter /> is not outputting anything. When I load the page, the after TextBlock appears directly below the before TextBlock with nothing in between. 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? My List object is simply a BindingList of some generic class that has a couple of public properties.

هل كانت مفيدة؟

المحلول

I found the answer. I should use <DataGridColumnHeadersPresenter /> instead of <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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top