zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    javascript入门教程笔记
    杭电2025
    杭电 2024
    杭电2019
    UEditor编辑器上传图片开发流程
    js操作textarea方法集合
    ueditor编辑器和at.js集成
    js分页算法
    js获取url中的参数
    第7章函数表达式笔记
  • 原文地址:https://www.cnblogs.com/key-ok/p/3531925.html
Copyright © 2011-2022 走看看