You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FireMonkey TListBoxGroupHeader点击等事件无法触发问题求助

解决TListBoxGroupHeader点击事件不触发的问题

问题原因

TListBoxGroupHeader是TListBoxItem的子类,但ListBox的事件处理机制会优先拦截所有鼠标事件,导致GroupHeader自身的OnClick等事件无法直接触发——ListBox会统一处理所有项的交互,而非让单个项的独立事件生效。

解决方案

方案1:利用ListBox的OnItemClick事件判断点击对象

这是最简便的实现方式,直接在ListBox的OnItemClick事件中判断被点击的项类型,再执行折叠/展开逻辑:

procedure TFrame_Logging.ListBox1ItemClick(const Sender: TObject; const AItem: TListBoxItem);
var
  GroupHeader: TListBoxGroupHeader;
begin
  if AItem is TListBoxGroupHeader then
  begin
    GroupHeader := TListBoxGroupHeader(AItem);
    // 切换组的折叠状态,ListBox会自动处理子项显示/隐藏
    GroupHeader.Expanded := not GroupHeader.Expanded;
  end;
end;

方案2:自定义TListBoxGroupHeader控件(进阶)

如果需要更灵活的自定义事件逻辑,可以继承TListBoxGroupHeader并手动触发点击事件:

type
  TMyListBoxGroupHeader = class(TListBoxGroupHeader)
  protected
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
  public
    property OnClick; // 确保OnClick属性可被绑定
  end;

procedure TMyListBoxGroupHeader.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  inherited;
  if Button = mbLeft then
    if Assigned(OnClick) then
      OnClick(Self);
end;

之后将设计界面中的TListBoxGroupHeader替换为自定义的TMyListBoxGroupHeader,即可正常绑定并触发OnClick事件。

方案3:通过ListBox的OnMouseDown结合HitTest定位项

若不想修改控件,可借助ListBox的鼠标事件和HitTest功能定位被点击的组头:

procedure TFrame_Logging.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
  HitInfo: TListBoxHitTestInfo;
  TargetItem: TListBoxItem;
begin
  if Button <> mbLeft then Exit;
  HitInfo := ListBox1.HitTest(X, Y);
  if HitInfo.Item <> nil then
  begin
    TargetItem := HitInfo.Item;
    if TargetItem is TListBoxGroupHeader then
    begin
      TListBoxGroupHeader(TargetItem).Expanded := not TListBoxGroupHeader(TargetItem).Expanded;
    end;
  end;
end;

补充说明

TListBoxGroupHeader自带Expanded属性,设置该属性后ListBox会自动处理组内项的显示与隐藏,无需手动遍历控制(仅在需要自定义显示逻辑时才需额外处理)。

内容的提问来源于stack exchange,提问作者Andre Coetzer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:22:48