2013-08-19 56 views
0

我想自定義一個DataGrid。我想在列標題上插入一些內容。我正在嘗試使用ControlTemplate來執行此操作。下面我有我的XAML代碼。我的問題是<ContentPresenter />不輸出任何東西。當我加載頁面時,after TextBlock出現在before TextBlock的正下方,兩者之間沒有任何內容。我想在該空間中顯示列標題。如何自定義WPF Datagrid中列標題的顯示?

<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和after TextBlock中的列標題?我的列表對象只是一些具有一些公共屬性的泛型類的BindingList。

回答

2

我找到了答案。我應該使用<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>