delphifiremonkey

Delphi FMX & ListBox & Styles


I have a pretty simple app but I can't get it to work properly (as I assume).

Here is styles I have added:

object TStyleContainer
  object TLayout
    StyleName = 'CustomItem_edit'
    Align = Center
    Padding.Left = 3.000000000000000000
    Padding.Top = 3.000000000000000000
    Padding.Right = 3.000000000000000000
    Padding.Bottom = 3.000000000000000000
    Size.Width = 296.000000000000000000
    Size.Height = 64.000000000000000000
    Size.PlatformDefault = False
    Visible = False
    TabOrder = 2
    object TText
      StyleName = 'text'
      Align = Top
      HitTest = False
      Position.X = 3.000000000000000000
      Position.Y = 3.000000000000000000
      Size.Width = 290.000000000000000000
      Size.Height = 17.000000000000000000
      Size.PlatformDefault = False
      TextSettings.FontColor = xFF909090
      TextSettings.WordWrap = False
      TextSettings.HorzAlign = Leading
    end
    object TEdit
      StyleName = 'Edit1'
      Touch.InteractiveGestures = [LongTap, DoubleTap]
      Align = Top
      TabOrder = 0
      Position.X = 3.000000000000000000
      Position.Y = 20.000000000000000000
      Size.Width = 290.000000000000000000
      Size.Height = 21.000000000000000000
      Size.PlatformDefault = False
    end
  end
  object TLayout
    StyleName = 'CustomItem_list'
    Align = Center
    Padding.Left = 3.000000000000000000
    Padding.Top = 3.000000000000000000
    Padding.Right = 3.000000000000000000
    Padding.Bottom = 3.000000000000000000
    Size.Width = 296.000000000000000000
    Size.Height = 64.000000000000000000
    Size.PlatformDefault = False
    Visible = False
    TabOrder = 0
    object TText
      StyleName = 'text'
      Align = Top
      HitTest = False
      Position.X = 3.000000000000000000
      Position.Y = 3.000000000000000000
      Size.Width = 290.000000000000000000
      Size.Height = 17.000000000000000000
      Size.PlatformDefault = False
      TextSettings.FontColor = xFF909090
      TextSettings.WordWrap = False
      TextSettings.HorzAlign = Leading
    end
    object TComboBox
      StyleName = 'ComboBox1'
      Align = Top
      Position.X = 3.000000000000000000
      Position.Y = 20.000000000000000000
      Size.Width = 290.000000000000000000
      Size.Height = 22.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
    end
  end
  object TLayout
    StyleName = 'CustomItem_label'
    Align = Center
    Padding.Left = 3.000000000000000000
    Padding.Top = 3.000000000000000000
    Padding.Right = 3.000000000000000000
    Padding.Bottom = 3.000000000000000000
    Size.Width = 296.000000000000000000
    Size.Height = 64.000000000000000000
    Size.PlatformDefault = False
    Visible = False
    TabOrder = 1
    object TText
      StyleName = 'text'
      Align = Top
      HitTest = False
      Position.X = 3.000000000000000000
      Position.Y = 3.000000000000000000
      Size.Width = 290.000000000000000000
      Size.Height = 17.000000000000000000
      Size.PlatformDefault = False
      Text = 'Text'
      TextSettings.FontColor = xFF909090
      TextSettings.WordWrap = False
      TextSettings.HorzAlign = Leading
    end
    object TLabel
      StyleName = 'Label1'
      Align = Top
      Position.X = 3.000000000000000000
      Position.Y = 20.000000000000000000
      Size.Width = 290.000000000000000000
      Size.Height = 17.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
    end
  end
end

Nothing wrong here probably. I'm able to add items to ListBox:

item := TListBoxItem.Create(nil);
item.Parent := ListBox1;
item.StyleLookup := 'CustomItem_label';
item.StylesData['text'] := ;
item.StylesData['Label1.Text'] := ;

But how change height of individual item (ideally based of Label1 content / height)?

Have tried a lot of attemps, seems not working: not working

item.Height := 1000;

not working

item.StylesData['CustomItem_label.Height'] := 1000;

not working also

obj := StyleBook1.Style.FindStyleResource('CustomItem_label');
if (obj is TLayout) then begin
  TLayout(obj).Height := 1000;
end;

What can be a reason?


Solution

  • I think you miss something, I made a little app to check (2 item style) I wrote this code

    procedure TForm2.btnAddItemClick(Sender: TObject);
    var newitem : TListBoxItem;
    begin
      ListBox1.BeginUpdate;
      newItem:=TlistBoxItem.Create(listbox1);
      if Switch1.IsChecked then begin
                               newItem.StyleLookup:='itemstyledual';
                               newitem.Height:=(Random(2)+1)*40;
                           end
                           else begin
                             newItem.StyleLookup:='itemstylesolo';
                           end;
      newitem.ApplyStyleLookup;
      newItem.text:='Item '+Listbox1.Items.Count.ToString; 
    // oops correct value should be Listbow1.Items.count+1 
      if Switch1.IsChecked then newitem.StylesData['extra']:='New item as '+newitem.Height.tostring+' Height';
      newitem.Parent:=Listbox1;
      listbox1.EndUpdate;
    end; 
    

    test app

    (2 first items at design time), Changing size works. To test : necessity of beginupdate/endupdate and newitem.applystylelookup even if, for me, it was a reflex (I don't work very often with customed Listbox)