为ListView Delphi XE7创建自定义项目外观 [英] Create a customized Item Appearance for ListView Delphi XE7

查看:673
本文介绍了为ListView Delphi XE7创建自定义项目外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi XE7中为TListView firemonkey控件创建自定义的项目外观,我遇到了很多麻烦。我想要的是为自己的设计定义一个项目应该是什么,并使用该项目。例如:

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 what an item should be and use that item. For example :

我想要一个带有标题的项目(上) - 描述(中) - 日期(底部) - 按钮(右)。

I would like to have an item with a Title(on top) - A Description(Middle) - A Date (Bottom) - Button(Right).

我找不到任何有关这方面的优秀文档,但我收到了如何使用muti详细信息创建TListView的示例,但问题是:该示例没有记录很难理解在那里发生什么。

I could not find any good documentation about this but i got some samples of how to create an TListView with muti details, but the problem is : that sample is not documented so is very hard to understand whats going on there.

我想要有一个链接或一些解释,如何做到这一点,或者如果另一种方式来取代我想。我试过使用TListBox,但在手机上的表现有点不好。我不得不说,我可以用TListBox做我想要的,但这是问题...表演。

I would like to have a link or some explanation of how to do this, or if theres other way to achive what I want. I have tried using TListBox but the performance on mobile is a little bad. I have to say that I could make what I want with TListBox, but this is the problem... The performance.

所以我想要一个控制列表

So I would like to have a control to list items(that I can create by my own) with a good performance.

推荐答案

A TListView

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.

Delphi内置的工具不一定允许您设计布局/模板在设计时(我听说过第三方库),但您仍然可以使用代码自定义。一个 TListView 实际上并不包含控件,而是一种特定类型的对象(继承自 TListItemObject )。这些是虚拟对象,旨在将最终的绘图中放置各种类型的数据。

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.

这是通过为 TListView.OnUpdateObjects 。这是您基本上设计布局的地方。以下是我在某些库存搜索结果中使用的一些代码:

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;

除了之外还有其他类似的类型TListItemText ,继承自 TListItemObject 。你甚至可以设计自己的,如果你需要。一旦你设计了这个布局,你需要填充内容...

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.

这篇关于为ListView Delphi XE7创建自定义项目外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆