【问题标题】:How do I customize the display of column headers in my WPF Datagrid?如何自定义 WPF Datagrid 中列标题的显示?
【发布时间】:2013-08-19 17:49:28
【问题描述】:

我正在尝试自定义 DataGrid。我想在列标题上方插入一些内容。我正在尝试使用 ControlTemplate 来执行此操作。我在下面有我的 XAML 代码。我的问题是<ContentPresenter /> 没有输出任何东西。当我加载页面时,after TextBlock 直接出现在 before TextBlock 下方,中间没有任何内容。我想在该空间中显示列标题。

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

如何在beforeTextBlock 和afterTextBlock 之间显示列标题?我的 List 对象只是一些具有几个公共属性的泛型类的 BindingList。

【问题讨论】:

    标签: wpf datagrid controltemplate contentpresenter


    【解决方案1】:

    我找到了答案。我应该使用&lt;DataGridColumnHeadersPresenter /&gt; 而不是&lt;ContentPresenter /&gt;。所以我的工作代码看起来像:

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

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多