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

查看:31
本文介绍了为 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).

我找不到关于此的任何好的文档,但我得到了一些有关如何创建具有多个详细信息的 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.

推荐答案

TListView 确实是当您有许多彼此具有相同布局的项目时使用的合适的东西(尽管可以使每个与下一个不同).TListBox 只在你没有太多项的情况下才有意义,每个项都需要有不同的内容(例如配置应用程序设置).我实际上刚刚修复了这个错误,将一些列表框切换到列表视图.

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;

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

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天全站免登陆