Customizing Column Cell Content

In this section you'll find information about changing the UI element shown as the content of cells belonging to a column when the cell is not in editing mode.

It’s important to note that cell content UI elements are recycled by the data grid; that means that this column could potentially use UI elements created by other columns.

To implement custom cell content you'll need to override the following methods:

      GetCellContentRecyclingKey: Key used to store the cell content for future reuse in a shared pool. Columns returning the same RecyclingKey will be candidates to share the same cell content instances.

      CreateCellContent: Creates the visual element that will be used to display the information inside a cell.

      BindCellContent: Initializes the cell content presenter. This method must set cellContent properties, the SetBinding of the corresponding dependency property being "row.DataItem", the source which can be set directly in the binding or in the DataContext of the cellContent.

      UnbindCellContent: This method is called before the cell content is recycled.

In the implementation of a hyperlink column the methods might look similar to the example below. In the following method a different key for this column is returned (the default key is typeof(TextBlock)), That means this column will not share the cell content element with other columns (unless it would be another column which returned the same key, but that's not likely to happen).

      Visual Basic

      C#

The CreateCellContent method will be called by the data grid if there is no recycled hyperlink. In this case a new hyperlink will be created which will be used in the cell once the cell that contains the hyperlink is unloaded; the hyperlink will be saved to be used in a future cell:

      Visual Basic

      C#

After the hyperlink is created or a recycled one is taken, the BindCellContent method will be called by the data grid passing the hyperlink as a parameter. In this method you should set the properties of the hyperlink to bind it to the data of the cell:

      Visual Basic

      C#

Note that you can also set the data item as the data context of the hyperlink instead of setting it in the Source property of the binding. For example:

      Visual Basic

      C#

Although you will end up with the same result, this technique is does not perform as well as setting the binding source property directly.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.