Create a customized Item Appearance for ListView Delphi XE7

梦想的初衷 提交于 2019-11-27 14:55:02

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!