TreeView垂直滚动条没有展开到最后一个节点,最后一个节点被隐藏了?
我遇到了 TreeView 控件的奇怪行为。
如您所见,树视图中的最后一个节点以某种方式隐藏(尽管我可以用键盘选择它)向上/向下箭头键)并且滚动条不会扩展到它。在图片中,您看到的蓝线是隐藏的选定节点,但幸运的是仍然有一点突出显示!
即使我按下翻页或END键,我也无法选择最后一个节点!
我尝试更改树视图控件的高度以匹配节点的高度,但仍然没有成功!我的树视图位于“面板”内。这可能是问题所在吗?
更新 我并没有做什么了不起的事情。我只是在 List<> 上循环填充(在循环之前和之后使用挂起和恢复)然后树视图将正常填充。
public class MyNode
{
public string Name {get;set;}
public string Result {get;set;}
}
//suspending code here (dont have access to paste it here)
foreach(MyNode node in myNodeList)
{
TreeNode tn = new TreeNode();
tn.Text = node.Name;
tn.Name = node.Result;
treeView.Nodes.Add(tn);
}
////unsuspending code here (dont have access to paste it here)
稍后,我允许用户按下按钮来突出显示结果设置为“失败”的节点:
foreach(TreeNode node in treeView.Nodes)
{
if (node.Name.ToString() == "fail") node.BackColor = Color.Red;
}
在此之后,树视图中的最后一个节点将被隐藏!
修复 我使用了 BeginUpdate() 和 EndUpdate() 方法,问题就消失了!!!
I am experiencing an odd behaviour from a TreeView control.
As you can see, the last node in the treeview is somehow hidden (although I can select it with keyboard up/down arrow keys) and the scroll bar does not expand to it. In the picture the blue line you see is the selected node that is hidden but luckily still a bit of highlight is visible!
Even when I press pagedown or END keys, I cannot select the last node!
I tried to change height of the treeview control to match the height of nodes but still no success! My treeview is inside a 'panel'. could it be the problem?
UPDATE
I am not doing something extraordinary. I just populate with a loop over a List<> (using suspend and resume before and after loop) then the treeview will be populated normally.
public class MyNode
{
public string Name {get;set;}
public string Result {get;set;}
}
//suspending code here (dont have access to paste it here)
foreach(MyNode node in myNodeList)
{
TreeNode tn = new TreeNode();
tn.Text = node.Name;
tn.Name = node.Result;
treeView.Nodes.Add(tn);
}
////unsuspending code here (dont have access to paste it here)
Later I allow user to press a button to highlight the nodes that have result set to 'fail':
foreach(TreeNode node in treeView.Nodes)
{
if (node.Name.ToString() == "fail") node.BackColor = Color.Red;
}
After this, the last node in the treeview is going hidden!!!
FIX
I used BeginUpdate() and EndUpdate() methods and the problem is gone!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现我必须在填充树视图以及对其节点进行任何更改之前和之后使用
treeView.BeginUpdate()
和treeView.EndUpdate()
方法。I found out that I have to user
treeView.BeginUpdate()
andtreeView.EndUpdate()
methods before and after populating the tree view and also making any changes to its nodes.您应该在添加节点之前和之后使用 SuspendLayout() 和 ResumeLayout()。
这将解决问题。
您可能还想在 SuspendLayout() 之前使用 BeginUpdate(),在 ResumeLayout() 之后使用 EndUpdate()。
编辑:这是 .Net 2.0 中的一个已知问题。
禁用视觉样式会有所帮助。
如果您不想禁用视觉样式,您可以在树的末尾添加一个空节点(当可见树视图控件中的节点过多时)。
请参阅 MS 中的问题:https://connect。 microsoft.com/VisualStudio/feedback/details/94021/treeview-does-not-display-the-last-node
You should use SuspendLayout() and ResumeLayout() before and after adding the nodes.
This will solve the problem.
You may also want to use BeginUpdate() before SuspendLayout() and EndUpdate() after ResumeLayout().
Edit: This is a known issue in .Net 2.0.
Disabling visual styles will help.
If you don't want to disable visual styles you could add an empty node to the end of the tree (when there are more than fit in the visible treeview control).
See the issue at MS: https://connect.microsoft.com/VisualStudio/feedback/details/94021/treeview-does-not-display-the-last-node