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
  • 相关阅读:
    Qt 模拟一个导航定位系统
    【编程之美】用C语言实现状态机(实用)
    代码面试之链表
    乾坤合一~Linux设备驱动之USB主机和设备驱动
    乾坤合一~Linux设备驱动之I2C核心、总线以及设备驱动
    乾坤合一~Linux设备驱动之终端设备驱动
    乾坤合一~Linux设备驱动之块设备驱动
    蜕变成蝶~Linux设备驱动之watchdog设备驱动
    蜕变成蝶~Linux设备驱动之按键设备驱动
    蜕变成蝶~Linux设备驱动之DMA
  • 原文地址:https://www.cnblogs.com/key-ok/p/3531925.html
Copyright © 2011-2022 走看看