Version

Styling the XamDataGrid

This topic is designed to get you up and running as quickly as possible with customizing the visual appearance of the XamDataGrid Control, by describing the basic steps required for styling textual and associated elements of the XamDataGrid control.

In this topic

Required Background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic provides basic steps required for adding the XamDataGrid control to your view and populating it with sample data.

This resource topic provides implementation of sample data that you can use a data source for the XamDataGrid control.

Styling Rows

The XamDataGrid control exposes the following properties for styling row elements:

XamDataGrid Styling behavior Property Details

RowHeight

Allows you to set the height for all rows within the XamDataGrid control

RowSeparatorHeight

Allows you to set the height of the separator bar which exists between individual rows

RowSeparatorStyle

Allows you to obtain a reference to the exact RowSeparator instance or to provide your own that will be used by the XamDataGrid control; allowing you to modify any of its appearance properties

The following example will walk you through modifying all these styling behaviors to customize the high level appearance of the XamDataGrid control.

  1. Add to the following code snippet to the main code file.

In XAML

<ig:XamDataGrid RowSeparatorHeight="4"
                RowHeight="50">
    <ig:XamDataGrid.RowSeparator>
        <ig:RowSeparator Background="Blue"/>
    </ig:XamDataGrid.RowSeparator>
</ig:XamDataGrid>

In C#:

DataGrid.RowSeparator = new RowSeparator();
DataGrid.RowSeparator.Background = new SolidColorBrush(Color.Blue);
DataGrid.RowSeparatorHeight = 5;
DataGrid.RowHeight = 50;
  1. Save and run your application to verify the change in appearance. The RowSeparator will appear with a blue background with a height of five pixels.

Styling the DataGridView 2.png

Styling Column Header

The XamDataGrid control’s Header object exposes several methods for modifying the visual appearance of its Header as well as its text.

Header Styling behavior Property Details

Height

Allows you to set the height of the header row; this row encompasses all column headers present on the XamDataGrid control

Background Color

Allows you to set the background color of the Header element of the Column

Font Family

Allows you to designate the font family used by text of the Column’s header

Font Size

Allows you to set the point size of the font used by the Column’s header

Font Style

Allows you to designate the style attribute of the font, for text within the Column’s header

Horizontal Alignment

Allows you to set the alignment of the text within the Column’s header, along the horizontal plane

Vertical Alignment

Allows you to set the alignment of the text within the Column’s header, along the vertical plane

Text Color

Allows you to specific Brush color to text contained within the Column’s header

Text Line Break Mode

Allows you to specific TextCellLineBreakMode enum to control how long text should be displayed within the Column’s header

Text Padding

Allows you to specific padding around text contained within the Column’s header

The following example will walk you through a basic scenario for customizing the appearance for the XamDataGrid control’s Column headers, by utilizing several appearance methods for the header element and the headers text.

  1. Add to the following code snippet to the main code file.

In C#:

TextColumn nameColumn = new TextColumn();
nameColumn.PropertyPath = "FirstName";
nameColumn.HeaderText = "FirstName";

NumericColumn salesColumn = new NumericColumn();
salesColumn.PropertyPath = "Sales";
salesColumn.HeaderText = "Sales";

TextColumn territoryColumn = new TextColumn();
territoryColumn.PropertyPath = "Territory";
territoryColumn.HeaderText = "Territory";

DataGrid.Columns.Add(nameColumn);
DataGrid.Columns.Add(salesColumn);
DataGrid.Columns.Add(territoryColumn);
  1. Create an instance of the TextHeader class with following properties:

    • font size - 15

    • background color - red

    • text color - white

In C#:

var header = new TextHeader();
header.Background = new SolidColorBrush(Color.Red);
header.TextColor = new SolidColorBrush(Color.White);
header.FontSize = 15;
nameColumn.Header = header;

Alternatively, you could also get the exact instance of the Header object that represents the nameColumn, and modify that instance directly; all property changes using this approach will propagate to the XamDataGrid control’s UI; the following code snippet demonstrates this approach.

In C#:

var header = nameColumn.Header;
header.Background = new SolidColorBrush(Color.Red);
header.TextColor = new SolidColorBrush(Color.White);
header.FontSize = 15;
  1. Save and run your application to verify the result, as demonstrated in the following screenshot.

Styling the DataGridView 3.png

Styling Column Content

The Column object also exposes several methods for modifying the visual components of its contained cells in aggregate, including the Column’s width, text alignment and text appearance.

Column Styling behavior Property * Details*

Content Opacity

Allows you to adjust the level of transparency for content, (including text and images) within cells of the Column

Width

Allows you to adjust the width for the Column

Background Color

Allows you to set a specific color that will be used to fill cells within the Column

Selected Background Color

Allows you to set a specific color for cells within a column, when that Column is selected

Font Family

Allows you to designate the FontFamily used by text of the Column’s cells

Font Size

Allows you to set the point size of the font used by the Column’s cells

Font Style

Allows you to designate the style attribute of the font, for text within the Column’s header

Horizontal Alignment

Allows you to set the alignment of the text within the Column’s cells, along the horizontal plane

Vertical Alignment

Allows you to set the alignment of the text within the Column’s cells, along the vertical plane

TextColor

Allows you to assign a specific Brush color to text contained within the Column’s cells

Text Line Break Mode

Allows you to specific TextCellLineBreakMode enum to control how long text should be displayed within the Column’s cells

Text Padding

Allows you to specific padding around text contained within the Column’s cells

This example walks through the customization of the appearance for columns within the XamDataGrid for column elements as well as the content contained within the column’s associated cells.

This example assumes you have created the SampleGridApp project as demonstrated in the Working with Columns topic.

  1. Add to the following code snippet to the main code file.

In C#:

TextColumn nameColumn = new TextColumn();
nameColumn.PropertyPath = "FirstName";
nameColumn.HeaderText = "FirstName";

NumericColumn salesColumn = new NumericColumn();
salesColumn.PropertyPath = "Sales";
salesColumn.HeaderText = "Sales";

TextColumn territoryColumn = new TextColumn();
territoryColumn.PropertyPath = "Territory";
territoryColumn.HeaderText = "Territory";

DataGrid.Columns.Add(nameColumn);
DataGrid.Columns.Add(salesColumn);
DataGrid.Columns.Add(territoryColumn);
  1. Modify the style of the nameColumn’s Column by setting the following properties:

    • background color - gray

    • text color - white

    • font style - bold

    • font size - 15

In C#:

nameColumn.FontSize = 15;
nameColumn.FontAttributes = FontAttributes.Bold;
nameColumn.Background = new SolidColorBrush(Color.Gray);
nameColumn.TextColor = new SolidColorBrush(Color.White);
  1. Save and run the SampleGridApp to verify the result.

Styling the DataGridView 4.png

Related Content

The following table lists topics that are related to this topic:

Topic Purpose

This topic provides information on supported column types in the XamDataGrid control.

This topic provides code examples on working with columns in the XamDataGrid control.