如何隐藏/删除WPF ColumnHeaderStyle列标题? [英] How can I hide/remove a WPF ColumnHeaderStyle column header?

查看:144
本文介绍了如何隐藏/删除WPF ColumnHeaderStyle列标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid ColumnHeaderStyle问题,就我而言,我想将这些组件(文本块,按钮)添加到所有列标题中,但是也生成了错误/伪造的列标题,因此出现带有其标题的列(Dosage + Btn ,Drug + Btn,Patient + Btn,Date + Btn),再加上没有标题名称的假列标题,该标题仅包含一个按钮-> + btn。
Datagrid没有预定义的列。
我的问题是如何隐藏/删除/禁用或取消此假列标题?
有什么建议吗?

I have a DataGrid ColumnHeaderStyle issue, in my case I wanted to add these components(textblock, button) to the all columns headers, but generated a wrong/fake column header too, so appear columns with their headers( Dosage + Btn, Drug + Btn, Patient + Btn, Date + Btn), and plus a fake column header with no header name, which contains only a button -> "" + btn. Datagrid has no predefined columns. My question is how can I hide/remove/disable or cancel this fake column header? Any suggests?

XAML:

<Window.Resources>

    <Style x:Key="dgheaderstyle" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Template">
         <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}" >
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="{Binding}" />
                        <Button Content="Click"></Button>
                    </StackPanel>

                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>

<Grid>
    <DataGrid ItemsSource="{Binding}"  ColumnHeaderStyle="{StaticResource dgheaderstyle}" HorizontalAlignment="Left" Height="262" Margin="10,21,0,0" VerticalAlignment="Top" Width="472">        
    </DataGrid>
</Grid>

后面的代码:

public partial class MainWindow : Window
{
   public MainWindow()
        {
            InitializeComponent();
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

            DataContext = table;
        }
 }


推荐答案

此是一个棘手的问题,请耐心等待。
额外的DataGridColumn标头命名为 PART_FillerColumnHeader
它放置在 PART_ColumnHeadersPresenter 中的ItemsPresenter后面。

This is a tricky issue so bear with me. The extra DataGridColumn header is named PART_FillerColumnHeader. It is placed behind the ItemsPresenter located within PART_ColumnHeadersPresenter.

DataGridTemplate的简化(不是

A simplification of a DataGridTemplate ( not really all it contains but just the relevant parts).

  <ControlTemplate>
      <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>

          <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" 
               Style="{StaticResource DataGridColumnHeaderStyle}"/>                                                                 
          <ScrollContentPresenter Grid.Row="1" x:Name="PART_ScrollContentPresenter" />                
     </Grid>      
  </ControlTemplate>

我们感兴趣的是 DataGridColumnHeadersPresenter ,让我们看看里面。
简体

our interest is in DataGridColumnHeadersPresenter , lets take a look inside. Simplified

 <Style x:Key="DataGridColumnHeadersPresenterStyle" TargetType="{x:Type DataGridColumnHeadersPresenter}">           
    <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridColumnHeadersPresenter}">
            <Grid>                                                                                                                          
               <DataGridColumnHeader x:Name="PART_FillerColumnHeader" IsHitTestVisible="False" />
               <ItemsPresenter />
            </Grid>
          </ControlTemplate>
       </Setter.Value>
    </Setter>
  </Style>

现在 DataGridColumnHeadersPresenter 的类型为DataGridColumnHeader。
将ColumnHeaderStyle分配给DataGrid时。

Now the ItemContainers of DataGridColumnHeadersPresenter are of type DataGridColumnHeader. When you assign a ColumnHeaderStyle to your DataGrid. It is applied to the Items of DataGridColumnHeaderPresenter and to PART_FillerColumnHeader AS WELL.

如上图所示, PART_FillerColumnHeader 放在后面,它适用于DataGridColumnHeaderPresenter项和 PART_FillerColumnHeader 。 ItemsPresenter。 ColumnHeadersPresenter的Items呈现到该ItemsPrsenter(意味着所有其他列标题)。这就是为什么您看到额外的列标题的原因。
您在列标题后面看到的额外的列标题。

As shown above PART_FillerColumnHeader is placed behind the ItemsPresenter. The Items of ColumnHeadersPresenter are rendered to that ItemsPrsenter (meaning all the other column headers).And that's why you see that extra column header. The extra column header you see behind your column headers.

现在如何摆脱它。简单为其指定一种样式。

Now how to get rid of it. simple assign it a style of it's own.

您只需使用Blend复制粘贴默认值即可。

You can just copy paste the default one using Blend.

这篇关于如何隐藏/删除WPF ColumnHeaderStyle列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆