In the WPF 4 DataGrid control (basic functionality) We've learned the basics of the DataGrid and how to use it. This article will continue with the custom DataGrid style, which will involve various style settings for ColumnHeader, Rowheader, Row, and Cell.
ColumnHeaderStyle Property
Generally speaking, the list header is the user's first attention, then how to design a beautiful head in the DataGrid. We can be defined globally by <DataGrid.ColumnHeaderStyle> in <DataGrid>, or on individual columns in <DataGrid.Columns> via < datagridcolumn.headerstyle> definition.
<datagrid x:name= "DataGrid" itemssource= "{Binding}" autogeneratecolumns= "False"
selectionunit= "Cellorrowheader" isreadonly= "True" >
<DataGrid.ColumnHeaderStyle>
<style targettype= "Datagridcolumnheader" >
<setter property= "Background" >
<Setter.Value>
<lineargradientbrush startpoint= "0,0" endpoint= "0,1" >
<gradientstop color= "White" offset= "0"/>
<gradientstop color= "Yellow" offset= "0.5"/>
<gradientstop color= "White" offset= "1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<setter property= "foreground" value= "Black"/>
<setter property= "fontsize" value= "/>"
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<datagridtextcolumn header= "name" binding= "{Binding Name}" >
<DataGridColumn.HeaderStyle>
<style targettype= "Datagridcolumnheader" >
<setter property= "Background" >
<Setter.Value>
<lineargradientbrush startpoint= "0,0" endpoint= "0,1" >
<gradientstop color= "White" offset= "0"/>
<gradientstop color= "Skyblue" offset= "0.5"/>
<gradientstop color= "White" offset= "1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<setter property= "foreground" value= "Black"/>
<setter property= "fontsize" value= "/>"
<setter property= "Width" value= "/>"
<Style.Triggers>
<trigger property= "IsMouseOver" value= "True" >
<setter property= "ToolTip" value= "Sort by Column"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridColumn.HeaderStyle>
</DataGridTextColumn>
<datagridtextcolumn header= ' age ' binding= ' {Binding age} ' >
<DataGridColumn.HeaderStyle>
<style targettype= "Datagridcolumnheader" >
<setter property= "Background" >
<Setter.Value>
<lineargradientbrush startpoint= "0,0" endpoint= "0,1" >
<gradientstop color= "White" offset= "0"/>
<gradientstop color= "LightGreen" offset= "0.5"/>
<gradientstop color= "White" offset= "1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<setter property= "foreground" value= "Black"/>
<setter property= "fontsize" value= "/>"
<setter property= "Width" value= "/>"
</Style>
</DataGridColumn.HeaderStyle>
</DataGridTextColumn>
<datagridcomboboxcolumn header= "Sex"
selecteditembinding= "{Binding Sex}"
Itemssource= ' {Binding source={staticresource sexenum} ' >
<DataGridColumn.HeaderStyle>
<style targettype= "Datagridcolumnheader" >
<setter property= "Background" >
<Setter.Value>
<lineargradientbrush startpoint= "0,0" endpoint= "0,1" >
<gradientstop color= "White" offset= "0"/>
<gradientstop color= "Lightpink" offset= "0.5"/>
<gradientstop color= "White" offset= "1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<setter property= "foreground" value= "Black"/>
<setter property= "fontsize" value= "/>"
<setter property= "Width" value= "/>"
</Style>
</DataGridColumn.HeaderStyle>
</DataGridComboBoxColumn>
<datagridcheckboxcolumn header= "pass Exam?" Width= "100"
Binding= "{Binding pass}"/>
<datagridhyperlinkcolumn header= "Email" width= "150"
binding= "{Binding Email}"/>
</DataGrid.Columns>
</DataGrid>