使用EasyListView调用LVItemGetCaption时显示项目?

时间:2011-03-18 04:11:56

标签: delphi listview virtual

我正在尝试使用EasyListview

实施虚拟数据模式

来自演示:

procedure TForm1.AddItems(Count: Integer);
var
  i: Integer;
begin
  // Add items to the listview. Actually the items are added to the first
  // group. This group is created automatically when the first item is added.
  LV.BeginUpdate;
  try
    for i := 0 to Count - 1 do
      LV.Items.AddVirtual;
  finally
    LV.EndUpdate;
  end;
end;

procedure TForm1.LVItemGetCaption(Sender: TCustomEasyListview;
  const Item: TEasyItem; Column: Integer; var Caption: WideString);
begin
  case Column of
    0: Caption := 'Item ' + IntToStr(Item.Index);
    1: Caption := 'Detail ' + IntToStr(Item.Index);
  end;
end;

如果我添加一些字符串:

procedure TForm1.AddItems(Count: Integer);
var
  i: Integer;
begin
  // Add items to the listview. Actually the items are added to the first
  // group. This group is created automatically when the first item is added.
  LV.BeginUpdate;
  try
    for i := 0 to Count - 1 do
    begin
      LV.Items.AddVirtual.Caption := 'DISPLAY ME ' + IntToStr(i);
    end;
  finally
    LV.EndUpdate;
  end;
end;

如何在调用LVItemGetCaption时获取并显示存储的虚拟标题(= string)?

如果我得到Caption := LV.Items.Items[Item.Index].Caption ;的标题,那么Stack溢出。

2 个答案:

答案 0 :(得分:1)

您必须将数据对象添加到项目中。 E.g:

type
  TMyData = class
    Caption: string;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  item: TEasyItemVirtual;
  MyData: TMyData;
begin
  EasyListview1.BeginUpdate;
  try
    for i := 0 to 100 - 1 do
    begin
      MyData := TMyData.Create;
      MyData.Caption := Format('My Item %D',[i]);
      item := EasyListview1.Items.AddVirtual;
      item.Data := MyData;
    end;
  finally
    EasyListview1.EndUpdate;
  end;
end;

procedure TForm1.EasyListview1ItemGetCaption(Sender: TCustomEasyListview; Item: TEasyItem;
  Column: Integer; var Caption: WideString);
begin
  case Column of
    0: Caption := TMyData(Item.Data).Caption;
    1: Caption := TMyData(Item.Data).Caption;
  end;
end;

不要忘记释放你的物品:

procedure TForm1.EasyListview1ItemFreeing(Sender: TCustomEasyListview; Item: TEasyItem);
begin
  if Assigned(Item.Data) then
    Item.Data.Free;
end;

答案 1 :(得分:0)

虚拟节点是不存储其数据的节点。它们只是视图您希望在程序的其他一些数据结构中已有的数据。当控件需要显示一个节点时,它会通过触发OnItemGetCaption事件向程序询问它应该使用哪个文本。

实际上,它会在需要知道Caption属性的值时调用该事件,因此当您尝试通过获取标题的值来处理caption-fetching事件时,会触发无限递归。