Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Cross-Platform-Entwicklung (https://www.delphipraxis.net/91-cross-platform-entwicklung/)
-   -   iOS Colorlistboxitemstyle bei ListBoxItems! Fehler bei mir oder in FMX? <- (https://www.delphipraxis.net/177676-colorlistboxitemstyle-bei-listboxitems-fehler-bei-mir-oder-fmx.html)

Mavarik 20. Nov 2013 13:01

Colorlistboxitemstyle bei ListBoxItems! Fehler bei mir oder in FMX? <-
 
Hallo Zusammen!

Ich erzeuge eine ListBox mit entsprechenden Items im Source dynamisch und setze den Style auf "colorlistboxitemstyle".

Wenn ich dann die Farbe (z.B. im OnClick) setze mit:
Delphi-Quellcode:
Procedure SetListBoxItemBarColor(LBItem : TListBoxItem;Color : TAlphaColor);
var
  ColorObj: TFmxObject;
begin
  ColorObj := LBItem.FindStyleResource('color');
  if ColorObj is TShape then
    TShape(ColorObj).Fill.Color := Color;
end;

procedure MyListBoxItemClick(Sender: TObject);
begin
  SetListBoxItemBarColor(TListBoxItem(Sender),SelectedGood);
end;
Wir die Farbe zwar gesetzt, aber wenn ich die Listbox scrolle werden beliebige Items mit der Farbe gesetzt.

Ist das ein Fehler im FMX oder muss ich bei mir weiter suchen?

Mavarik

RWarnecke 20. Nov 2013 16:21

AW: Colorlistboxitemstyle bei ListBoxItems! Fehler bei mir oder in FMX? <-
 
Wie sieht den die Procedure aus, um die Items zu erstellen ? Ich erstelle immer die Items in der Listbox und mache dann ganz zum Schluß ein RealignContent auf die Listbox. Vielleicht hilft das ja.

moe120 21. Nov 2013 11:18

AW: Colorlistboxitemstyle bei ListBoxItems! Fehler bei mir oder in FMX? <-
 
das klingt stark nach nem FMX Bug, habe etwas ähnliches mit Checkboxen die zur Laufzeit raufgepackt werden, sobald man scrollt verseuchen diese auch andere Elemente in der Listbox. (siehe auch http://qc.embarcadero.com/wc/qcmain.aspx?d=119638)

mach am besten mal nen QC Eintrag dazu auf, von wegen steter Tropfen und so :)

aber ein:
ListBox.BeginUpdate;
[Einfärbung];
ListBox.EndUpdate;
ListBox.RealignContent;

würde ich auch mal probieren

Olli73 24. Feb 2015 03:50

AW: Colorlistboxitemstyle bei ListBoxItems! Fehler bei mir oder in FMX? <-
 
Hallo!

Der Thread ist zwar schon etwas älter, aber da mich dieses Problem gerade die ganze Nacht gekostet hat und ich außer dem Beitrag hier nicht viel (insbesondere keine Lösung) dazu gefunden habe, möchte ich euch meine Lösung nicht vorenthalten. Vielleicht kann ich ja dazu beitragen, dass nicht irgendwann mal noch jemand anderes eine schlaflose Nacht hat.

Der "Trick" ist eigentlich nur, dass man die Farbe im "OnPainting" der Items setzen muss:

Delphi-Quellcode:
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.
Gruß,
Olli


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:26 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz