【问题标题】:TListView groups and items do not appear after clearing and reloading groups and items清除和重新加载组和项目后,TListView 组和项目不出现
【发布时间】:2014-03-12 17:19:54
【问题描述】:

Delphi XE4 更新 1 和 Windows 8。

当我将组和项目添加到列表视图时,它们会正确显示。然后,当我清除项目和组并再次添加它们时,什么也没有出现。这肯定不是预期的行为吗?

来自 DFM:

object lv: TListView
  Left = 24
  Top = 20
  Width = 250
  Height = 225
  Columns = <
    item
      Caption = 'Model'
      Width = 180
    end>
  GroupView = True
  ReadOnly = True
  RowSelect = True
  TabOrder = 0
  ViewStyle = vsReport
end

代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  LListGroup: TListGroup;
  LListItem: TListItem;
begin
  lv.Items.Clear;
  lv.Groups.Clear;

  LListGroup := lv.Groups.Add;
  LListGroup.Header := 'Ford';

  LListItem := lv.Items.Add;
  LListItem.Caption := 'Escape';
  LListItem.GroupID := LListGroup.ID;

  LListItem := lv.Items.Add;
  LListItem.Caption := 'F150';
  LListItem.GroupID := LListGroup.ID;

  OutputDebugString(PChar(Format('lv.Groups.Count=%d', [lv.Groups.Count])));
end;

我第一次单击按钮时,项目会出现并且它们被分组。第二次,列表视图为空白。如果我注释掉清除组的行,那么它可以工作,但是组的数量,所有这些都未使用,只有一个,每次都会增加 1。

【问题讨论】:

    标签: delphi tlistview


    【解决方案1】:

    您的代码的问题是您将TCollectionItemID 属性传递给TListItemGroupID 属性,并且您必须使用TListGroupGroupID 属性。

    所以改变这一行

      LListItem.GroupID := LListGroup.ID; //here you are passing a wrong id for the group
    

      LListItem.GroupID := LListGroup.GroupID; //This is a valid assignment for the GroupID property
    

    【讨论】:

    • 就是这样!非常明显,但我无法告诉你我看了多少次却没有看到它。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-12-07
    • 2013-12-15
    • 2016-02-18
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多