[英]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.