Custom Grid Columns

原文

  http://monkeystyler.com/guide/Custom-Grid-Columns

ack to FireMonkey Topics

As we saw in TGrid a FireMonkey grid consists of columns which contain cells made of any descendant of TStyledControl. Or, effectively, any control. A number of column types come built in but it is a pretty simple matter to create your own. Here we’ll create a column of TColorComboBoxes.

Cells

Firstly you’ll need a class to use for the cells, which we’ll call TColorComboCell.

type TColorComboCell = class(TColorComboBox)   end

You could just use the base class (TColorComboBox) directly but creating an explicit class for the cells adds benefits such as the ability to change styles more easily (just create a style called ColorComboCellStyle) and we can more easily modify behavious needed specific to a grid cell.

If you’re creating your own control from scratch to use as a cell you’ll need to implement GetData and SetData methods of TStyledControl since these are used by the grid for reading and writing data to the cell.

The Column

Now we need a custom column class which we’ll call TColorComboColumn. In the column class we need to override the CreateCellControl function which creates individual cells.

function TColorComboColumn.CreateCellControlTStyledControl; begin   Result := TColorComboCell.Create(Self);   TColorComboBox(Result).OnChange := DoTextChanged; end

Three things to note here:

  • We don’t need to set Parent, this will be set by the column.
  • If your cell wants to pass data back to your application you’ll need to hook into the cell’s OnChange or similar event so the grid will fire it’s OnSetValue and OnEditingDone events. If the name of the method I’m hooking up sounds inappropriate it’s down to the slightly oddball class hierarchy of columns where TColumn, for string data, acts as the base class for all column classes, rather than using an abstract class.
  • There’s no need to call the inherited method. Indeed, you positively don’t want to since doing so will leave you with an unwanted TTextCell. I apologise if this seems messy, but again it’s all down to that strange class heirarchy.

Round Up

And that’s really all there is to it. You can now add your column to a grid with the line

Grid1.AddObject(TColorComboColumn.Create(Grid1)); 

Full Source

unit ColorComboColumn;
interface
uses FMX.ColorsFMX.GridFMX.Controls;
type TColorComboCell = class(TColorComboBox)   end;
type TColorComboColumn = class(TColumn)   protected     function CreateCellControlTStyledControl;override;   end;
implementation
{ TColorComboColumn }
function TColorComboColumn.CreateCellControlTStyledControl; begin   Result := TColorComboCell.Create(Self);   TColorComboBox(Result).OnChange := DoTextChanged; end;
end
原文地址:https://www.cnblogs.com/key-ok/p/3531925.html