TreeView(带复选框)无法正确处理点击?

Tim*_*ery 1 c# treeview events double-click

我有一个TreeList,它基本上就像一个Photoshop图层调色板.它是一个分层列表,其中包含用于切换文档各个图层可见性的复选框.这是通过BeforeChecked事件完成的,该事件是在.Checked值切换之前引发的.

它工作正常,除非你双击它,此时它似乎都变得混乱.

If you double-click a checkbox once, it toggles the checked value twice (which is the intended behavior), but it doesn't toggle the visibility of the layer twice because it doesn't raise the BeforeChecked twice.

I figured I'd get around this by putting this in the MouseDoubleClick event:

TreeViewHitTestInfo hit = treeLayerPalette.HitTest(e.X, e.Y);
hit.Node.Checked = !(hit.Node.Checked);
Run Code Online (Sandbox Code Playgroud)

This works for all double-clicks except for the first one. So it only raises the BeforeChecked event once (and not the MouseDoubleClick) at first, getting the checkbox out of sync with the visibility of the layer, and then all following double-clicks raise both the BeforeChecked and MouseDoubleClick events (which in turn raises the BeforeChecked event), maintaining that incorrect relationship.

Also, at one point, I put a MessageBox.Show() in the DoubleClick event. Awkwardly enough, it does not actually get shown on a double-click, but instead gets shown on a third click, no matter how much time has elapsed between the actual double-click and the third click. A third click performed 20 seconds after a double-click will raise the MouseDoubleClick event, but the actual double-click won't.

What's actually going on here, and how can I fix it?

小智 9

这是Checkbox启用的树视图的问题,但是有一些可接受的工作区.首先:MS知道问题但拒绝修复它...:http://connect.microsoft.com/VisualStudio/feedback/details/ 775922/treeview-double-click-bug#details < - Bug报告....

所以除了解决它之外别无选择..最简单的方法是继承Treeview并强制禁用复选框上的dblclick ...回答(在SOF上):c#treeview ignore双击只复制复选框

希望这可以帮助....

  • 啊啊!很高兴听到我不只是疯了而且是别人的错误而不是我的错误,改变了.子类解决方案工作得很好,谢谢! (2认同)