Create a customized Item Appearance for ListView Delphi XE7

前端 未结 1 673
清歌不尽
清歌不尽 2020-12-03 09:41

Im having a lot of trouble trying to create a customized item appearance for the TListView firemonkey control for Delphi XE7. What I want is to define my own \"design\" for

相关标签:
1条回答
  • 2020-12-03 10:10

    A TListView is indeed the appropriate thing to use when you have many items which are to have the same layout as each other (although it is possible to make each one vary from the next). A TListBox is only meant when you have not too many items, and each one needs to have different contents (such as configuring application settings). I actually just got done fixing this mistake, switching some List Boxes to List Views.

    The tools built-in to Delphi don't necessarily allow you to design a layout / template in design-time (I've heard of third-party libraries for this), however you can still customize it with code. A TListView does not actually contain controls inside - instead a particular type of object (inherited from TListItemObject). These are virtual objects meant to place various types of data in the final drawing.

    This starts by adding an event handler for TListView.OnUpdateObjects. This is where you essentially "design" the layout. Here's some code which I'm using for example in some inventory search results:

    procedure TfrmInventoryContent.lstItemsUpdateObjects(const Sender: TObject; const AItem: TListViewItem);
    var
      TextLabel: TListItemText;
    begin
      //Add objects per item in list view for displaying more info
    
      //Item Price Label
      TextLabel := AItem.Objects.FindObject('lblPrice') as TListItemText;
      if TextLabel = nil then begin
        TextLabel:= TListItemText.Create(AItem);
        TextLabel.Name:= 'lblPrice';
        TextLabel.Align:= TListItemAlign.Trailing;
        TextLabel.VertAlign:= TListItemAlign.Leading;
        TextLabel.TextAlign:= TTextAlign.Trailing;
        TextLabel.PlaceOffset.X:= -10;
        TextLabel.PlaceOffset.Y:= 4;
        TextLabel.Font.Size:= 14;
        TextLabel.Width:= 60;
        TextLabel.Height:= 18;
        TextLabel.Text:= '';
        TextLabel.TextColor:= TAlphaColorRec.Green;
      end;
      //Item Quantity Label
      TextLabel := AItem.Objects.FindObject('lblQty') as TListItemText;
      if TextLabel = nil then begin
        TextLabel:= TListItemText.Create(AItem);
        TextLabel.Name:= 'lblQty';
        TextLabel.Align:= TListItemAlign.Trailing;
        TextLabel.VertAlign:= TListItemAlign.Leading;
        TextLabel.TextAlign:= TTextAlign.Trailing;
        TextLabel.PlaceOffset.X:= -120;
        TextLabel.PlaceOffset.Y:= 4;
        TextLabel.Font.Size:= 14;
        TextLabel.Width:= 30;
        TextLabel.Height:= 18;
        TextLabel.Text:= '';
        TextLabel.TextColor:= TAlphaColorRec.Blue;
      end;
    end;
    

    There are other similar types other than just TListItemText, inheriting from TListItemObject. You can even design your own if you need to. Once you have designed this layout, you then need to populate the contents...

    var
      TextLabel: TListItemText;
      I: TListViewItem;
    begin
      //Assuming I is already added to list somewhere
      TextLabel := I.Objects.FindObject('lblPrice') as TListItemText;
      if Assigned(TextLabel) then begin
        TextLabel.Text:= FormatFloat('$#,##0.00', InventoryItem.CustomerPrice.Price);
      end;
    
      TextLabel := I.Objects.FindObject('lblQty') as TListItemText;
      if Assigned(TextLabel) then begin
        TextLabel.Text:= IntToStr(InventoryItem.Quantity);
      end;
    end;
    

    Note how each of these objects has a "name" which is to be unique (but doesn't follow the same component names you're already used to). These names are unique to each list item.

    0 讨论(0)
提交回复
热议问题