WPF DataGrid 标题样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32975917/
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
WPF DataGrid Header styling
提问by Balázs Kóródi
I want to create a custom styled header for WPF DataGrid, but I don't know if it is possible to do what I want, and if it is how exactly should I do.
我想为 WPF DataGrid 创建一个自定义样式的标题,但我不知道是否可以做我想做的,以及我应该如何做。
The sketch of the DataGrid is on the linked picture. The Purple header extends beyond the edge of the DataGrid and it has a little 3D bending.
DataGrid 的草图位于链接图片上。紫色标题超出了 DataGrid 的边缘,并且有一点 3D 弯曲。
Can I do something like this with WPF DataGrind and if yes how do I start?
我可以用 WPF DataGrind 做这样的事情,如果是,我该如何开始?
Thanks!
谢谢!
回答by AnjumSKhan
DataGrid allows for ColumnHeaderStyle, CellStyle and many more styles to bet set. Right click DataGrid control in designer view > View Additional Templates will show you the complete list.
DataGrid 允许设置 ColumnHeaderStyle、CellStyle 和更多样式。在设计器视图中右键单击 DataGrid 控件 > 查看附加模板将显示完整列表。
You have to use the following :
您必须使用以下内容:
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Orange"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="0 0 3 0"/>
</Style>
This will change the column headers. And if you want to change cells too, you can provide styles for DataGridCell too.
这将更改列标题。如果您也想更改单元格,您也可以为 DataGridCell 提供样式。
Use Snoop tool to peek inside a DataGrid first and peek into it to see what it looks like at runtime. This will clear many of your concepts.
首先使用 Snoop 工具查看 DataGrid 内部,然后查看它在运行时的外观。这将清除您的许多概念。
回答by user3690202
It is possible, but it is a lotof work and requires very good knowledge of writing WPF templates. What you essentially want to do is replace the templates for DataGrid, and all other DataGrid related conotrols such as the DataGridRow, etc
这是可能的,但它需要大量的工作,并且需要非常好的编写 WPF 模板的知识。您本质上想要做的是替换 DataGrid 的模板,以及所有其他与 DataGrid 相关的控件,例如 DataGridRow 等
Microsoft provide a full example of how to do this here: https://msdn.microsoft.com/en-us/library/vstudio/ff506248(v=vs.100).aspx
Microsoft 在此处提供了如何执行此操作的完整示例:https: //msdn.microsoft.com/en-us/library/vstudio/ff506248(v=vs.100).aspx

