Custom Cells > Custom Cells in code: CellFactory class |
The grid has a CellFactory class that creates every cell shown on the grid. To create custom cells, create a class that implements the ICellFactory interface and assign this class to the grid's CellFactory property.
Like custom columns, custom ICellFactory classes can be highly specialized and application-specific, or they can be general, reusable, configurable classes. In general, custom ICellFactory classes are a lot simpler than custom columns since they deal directly with cells (columns, by contrast, need to deal with the columns themselves and also with the cells and other objects contained in the column).
The ICellFactory interface looks like this:
C# |
Copy Code
|
---|---|
public interface ICellFactory { FrameworkElement CreateCell( C1FlexGrid grid, CellType cellType, CellRange range); FrameworkElement CreateCellEditor( C1FlexGrid grid, CellType cellType, CellRange range) void DisposeCell( C1FlexGrid grid, CellType cellType, FrameworkElement cell); } |
The first method, CreateCell, creates FrameworkElement objects to represent cells. The parameters include the grid that owns the cells, the type of cell to create, and the CellRange to represent. The CellType parameter specifies whether the cell being created is a regular data cell, a row or column header, or the fixed cells at the top left and bottom right of the grid.
The second method, CreateCellEditor, is analogous to the first but creates a cell in edit mode.
The last method, DisposeCell, is called after the cell is removed from the grid. If gives the caller a chance to dispose of any resources associated with the cell object.
When using custom cells, it is important to understand that grid cells are transient. Cells are constantly created and destroyed as the user scrolls, sorts, or selects ranges on the grid. This process is known as virtualization and is quite common in Silverlight and WPF applications. Without virtualization, a grid would typically have to create several thousand visual elements at the same time, which would ruin performance.
Implementing custom ICellFactory classes is fairly easy because you can inherit from the default CellFactory class included with the C1FlexGrid. The default CellFactory class was designed to be extensible, so you can let it handle all the details of cell creation and customize only what you need.
The iTunes Sample and Financial Application Sample are examples of grids that use custom ICellFactory classes to implement functionality. The examples are part of the MainTestApplication included with this document, which includes both Silverlight and WPF versions of every sample. The descriptions in this document focus on key implementation points. For details, please refer to the sample application source code.