如何隐藏 WPF DataGrid 中的列标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1075902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:40:06  来源:igfitidea点击:

How to hide the Column header in a WPF DataGrid?

wpfdatagridexpression-blendexpression

提问by Ubalo

I am using a DataGrid in Expression Blend but I just need to show only the registries and hide the ColumnHeader.

我在 Expression Blend 中使用 DataGrid,但我只需要显示注册表并隐藏 ColumnHeader。

How do I do that?

我怎么做?

回答by Vic

In the DataGridthere is a Header section where the field Header Visibilitycould be set to None.

DataGrid有一个 Header 部分,该字段Header Visibility可以设置为None.

Or in xaml for the Datagridadd the property

或者在 xaml 中Datagrid添加属性

HeadersVisibility="None"

回答by HCP

Both DataGridof namespace System.Windows.Controls and WpfToolkit:DataGridof Microsoft.Windows.Controls have the property

这两个数据网格命名空间System.Windows.Controls和 WpfToolkit:DataGrid中Microsoft.Windows.Controls有物业

HeadersVisibility="None"

In the properties window of DataGrid you can indeed choose from the available options

在 DataGrid 的属性窗口中,您确实可以从可用选项中进行选择

None
Column
Row
All

But this doesn't appear in the Properties window of WpfToolkit:DataGridSo as far as I know, you need to type that in inside your .xaml file.

但这不会出现在WpfToolkit:DataGrid的“属性”窗口中 ,据我所知,您需要在 .xaml 文件中输入它。

<WpfToolkit:DataGrid HeadersVisibility="None">
       ...
</WpfToolkit:DataGrid>

If you want I can post the whole UserControl. Is that useful ?

如果你愿意,我可以发布整个 UserControl。那有用吗?

回答by HCP

This may be double posted, SO is being weird, but you can do this from code behind too.

这可能是双重发布的,所以很奇怪,但是您也可以从后面的代码中执行此操作。

C# code behind with a XAML datagrid named dg_Main would be:

带有名为 dg_Main 的 XAML 数据网格的 C# 代码将是:

dg_Main.HeadersVisibility = Microsoft.Windows.Controls.DataGridHeadersVisibility.None;

Doing this in code behind makes it easy to dynamically show and hide headers as needed.

在后面的代码中这样做可以很容易地根据需要动态显示和隐藏标题。