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

如何阻止TVirtualStringTree点击项后自动滚动回顶部

TVirtualStringTree点击项自动滚动回顶部问题解决

问题描述

每次点击TVirtualStringTree中的项时,树控件都会自动滚动回到顶部,已配置的控件属性如下:

object vtCodeExplr: TVirtualStringTree
  Left = 0
  Top = 50
  Width = 441
  Height = 1336
  HelpContext = 22
  Margins.Left = 5
  Margins.Top = 5
  Margins.Right = 5
  Margins.Bottom = 5
  Align = alClient
  BevelEdges = []
  BevelInner = bvNone
  BevelOuter = bvNone
  BorderStyle = bsNone
  Colors.BorderColor = 15987699
  Colors.DisabledColor = clGray
  Colors.DropMarkColor = 15385233
  Colors.DropTargetColor = 15385233
  Colors.DropTargetBorderColor = 15385233
  Colors.FocusedSelectionColor = 15385233
  Colors.FocusedSelectionBorderColor = 15385233
  Colors.GridLineColor = 15987699
  Colors.HeaderHotColor = clBlack
  Colors.HotColor = clBlack
  Colors.SelectionRectangleBlendColor = 15385233
  Colors.SelectionRectangleBorderColor = 15385233
  Colors.SelectionTextColor = clBlack
  Colors.TreeLineColor = 9471874
  Colors.UnfocusedColor = clGray
  Colors.UnfocusedSelectionColor = 13421772
  Colors.UnfocusedSelectionBorderColor = 13421772
  DefaultNodeHeight = 27
  Header.AutoSizeIndex = 0
  Header.Height = 29
  Header.MainColumn = -1
  Header.MaxHeight = 15000
  Header.MinHeight = 15
  HintMode = hmHint
  Images = vilItemImages16x16
  Indent = 27
  LineStyle = lsSolid
  Margin = 6
  TabOrder = 0
  TabStop = False
  TextMargin = 6
  TreeOptions.PaintOptions = [toShowButtons, toShowRoot, toShowTreeLines, toThemeAware, toUseBlendedImages]
  OnClick = vtCodeExplrClick
  OnCollapsed = vtCodeExplrCollapsed
  OnCompareNodes = vtCodeExplrCompareNodes
  OnDblClick = vtCodeExplrDblClick
  OnExpanded = vtCodeExplrExpanded
  OnFreeNode = vtCodeExplrFreeNode
  OnGetText = vtCodeExplrGetText
  OnGetImageIndex = vtCodeExplrGetImageIndex
  OnGetHint = vtCodeExplrGetHint
  OnInitNode = vtCodeExplrInitNode
  Touch.InteractiveGestures = [igPan, igPressAndTap]
  Touch.InteractiveGestureOptions = [igoPanSingleFingerHorizontal, igoPanSingleFingerVertical, igoPanInertia, igoPanGutter, igoParentPassthrough]
  Columns = <>
end

排查与解决步骤

  • 检查点击事件逻辑
    你绑定了vtCodeExplrClick点击事件,如果事件内调用了ReinitTree、FullRepaint或者全量遍历刷新所有节点的逻辑,会触发树控件重置滚动位置回到顶部。如果只需要修改点击节点的状态,仅调用InvalidateNode(目标节点)局部刷新即可,不要全量刷新控件。
  • 补充树控件配置项
    你当前仅配置了TreeOptions.PaintOptions,需要额外修改另外两个配置组:
    1. 移除TreeOptions.SelectionOptions中的toScrollToFocusedNode选项,避免每次选中节点时自动滚动到视图顶部
    2. 在TreeOptions.BehaviorOptions中添加toDisableAutoscrollOnFocus选项,直接禁止焦点变更时的自动滚动行为
      配置示例:
    vtCodeExplr.TreeOptions.SelectionOptions := vtCodeExplr.TreeOptions.SelectionOptions - [toScrollToFocusedNode];
    vtCodeExplr.TreeOptions.BehaviorOptions := vtCodeExplr.TreeOptions.BehaviorOptions + [toDisableAutoscrollOnFocus];
    
  • 检查动态加载逻辑
    你当前属性中Columns = <>,说明列是动态生成的。如果点击节点时你调用了Header.AutoFitColumns、Clear后重新添加列/节点的逻辑,也会触发滚动位置重置。建议只在窗体初始化/数据源全量变更时才重建列和节点,普通点击操作不要触发重建逻辑。
  • 排查触摸手势冲突
    你开启了igPan平移手势,如果是触屏设备上操作,手势事件和点击事件冲突也可能触发异常滚动,可以临时注释掉Touch.InteractiveGestures中的igPan相关配置测试是否恢复正常。

内容的提问来源于stack exchange,提问作者John Barrat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 15:06:00