WPF 4 DataGrid Control (custom style article)

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.