unit UfrmListBoxColored;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Forms, FMX.Graphics, FMX.StdCtrls, FMX.ListBox, FMX.Objects, FMX.Layouts,
FMX.Types, FMX.Controls, FMX.Dialogs;
type
TfrmListBoxColored =
class(TForm)
btnAddItem: TButton;
lbxColored: TListBox;
procedure btnAddItemClick(Sender: TObject);
procedure lbxColoredItemClick(
const Sender: TCustomListBox;
const Item: TListBoxItem);
public
procedure ListBoxItemPainting(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
end;
var
frmListBoxColored: TfrmListBoxColored;
implementation
{$R *.fmx}
procedure TfrmListBoxColored.btnAddItemClick(Sender: TObject);
var
idx: Integer;
LBI: TListBoxItem;
begin
idx := lbxColored.Items.Add('
X');
LBI := lbxColored.ItemByIndex(idx);
LBI.ItemData.Accessory := TListBoxItemData.TAccessory.aNone;
LBI.StyleLookup := '
colorlistboxitemstyle';
LBI.Tag := 1;
LBI.ItemData.Text := Format('
Item %.3d [%d]', [idx, LBI.Tag]);
LBI.OnPainting := ListBoxItemPainting;
end;
procedure TfrmListBoxColored.lbxColoredItemClick(
const Sender: TCustomListBox;
const Item: TListBoxItem);
begin
if Item.Tag = 1
then begin
Item.Tag := 2;
end else begin
Item.Tag := 1;
end;
Item.ItemData.Text := Format('
Item %.3d [%d]', [Item.
Index, Item.Tag]);
Item.Repaint;
end;
procedure TfrmListBoxColored.ListBoxItemPainting(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
var
LBI: TListBoxItem;
begin
LBI := (Sender
as TListBoxItem);
if LBI.Tag = 2
then begin
(LBI.FindStyleResource('
color')
as TRectangle).Fill.Color := TAlphaColorRec.Green;
end else begin
(LBI.FindStyleResource('
color')
as TRectangle).Fill.Color := TAlphaColorRec.Red;
end;
end;
end.