简体   繁体   English

如何通过单击空白背景来选择任何内容?

[英]How to select nothing by clicking the empty background?

I have a Virtual Treeview (eg TVirtualStringTree ). 我有一个虚拟树视图(例如TVirtualStringTree )。

  • The user can select a row 用户可以选择一行
  • but it would be nice if they could also do the intuitiave thing of clicking "nowhere" to select no row 但是如果他们也可以点击“无处”来选择没有行的直觉

在此输入图像描述

Note: Of course multiselect is off; 注意:当然多选是关闭的; because they can only select zero or one items 因为他们只能选择零个或一个项目

MCRE: MCRE:

procedure TForm6.FormCreate(Sender: TObject);
var
    vst: TVirtualStringTree;
begin
    vst := VirtualStringTree1;

    vst.RootNodeCount := 5;
    vst.TreeOptions.SelectionOptions := vst.TreeOptions.SelectionOptions + [toFullRowSelect];
    vst.Header.Options := vst.Header.Options + [hoVisible];
    vst.Header.Columns.Add;
    vst.Header.Columns.Add;
    vst.Header.Columns.Add;
    vst.Header.Columns.Add;
    vst.Header.Columns.Add;
end;

This should work out-of-the-box if toAlwaysSelectNode is not set and toMultiSelect is set in TreeOption.SelectionOptions. 如果没有设置toAlwaysSelectNode并且在TreeOption.SelectionOptions中设置了toMultiSelect,那么这应该是开箱即用的。 Tested wit latest source. 测试了最新的来源。

In other cases simply call ClearSelection() : 在其他情况下,只需调用ClearSelection()

procedure TVisibilityForm.VST2MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if TBaseVirtualTree(Sender).GetNodeAt(Point(X, Y)) = nil then
    TBaseVirtualTree(Sender).ClearSelection();
end;

This procedure in OnMouseDown should work regardless of the settings, you just need toRightClickSelect in TreeOptions.SelectionsOptions for right click selection, otherwise it doesn't work properly. 此过程中OnMouseDown无论怎样设置应该工作,你只需要toRightClickSelectTreeOptions.SelectionsOptions右点击选择,否则就不能正常工作。

procedure VSTMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button in [mbLeft, mbRight] then
    VST.FocusedNode := VST.GetNodeAt(X, Y);

  if Assigned(VST.FocusedNode) then
    VST.TreeOptions.PaintOptions := VST.TreeOptions.PaintOptions - [toAlwaysHideSelection]
  else
    VST.TreeOptions.PaintOptions := VST.TreeOptions.PaintOptions + [toAlwaysHideSelection];
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM