0
我有一個TreeView有很多節點。如果我切換節點,樹視圖的滾動條被設置在底部。Treeview保持滾動位置
爲了保持切換節點可見,我使用了node.EnsureVisible()
。但我不太喜歡這種方法,因爲它讓最終用戶感到困惑。
所以我看得更遠,現在我使用的是這裏提供的代碼:
Maintain scroll position of treeview
這段代碼的問題是,樹視圖的內容不滾動。滾動條位置正確,但內容不起任何作用。直到我點擊滾動條(我不滾動)內容變得可見。
所以,我想要實現的是當treenode被切換時,我想保持滾動位置。
切換節點的代碼。在這種情況下,節點向下。該功能是這樣的:
// Check a node is selected
if (tvCategories.SelectedNode == null)
return;
// The first node may not be moved
if (IsNewRootCategorySelected())
return;
Point ScrollPos = GetTreeViewScrollPos(tvCategories);
// Declare and instantiate the parent node
TreeNodeCollection parent = null;
if (tvCategories.SelectedNode.Parent == null)
parent = tvCategories.Nodes;
else
parent = tvCategories.SelectedNode.Parent.Nodes;
TreeNode selectedNode = tvCategories.SelectedNode;
int index = selectedNode.Index;
// Check there's a next node at the same level
if (tvCategories.SelectedNode.NextNode == null)
{
// Check if the parent node has a next node
if (tvCategories.SelectedNode.Parent != null && tvCategories.SelectedNode.Parent.NextNode != null)
{
// get the destination parent
TreeNode destParent = selectedNode.Parent.NextNode;
// remove selected node from tree view
parent[index].Remove();
// If selected node is a category, add the node to the first index
if (selectedNode.Tag is Category)
{
destParent.Nodes.Insert(0, selectedNode);
}
// If selected node is a question, add node below last category
if (selectedNode.Tag is Question)
{
int newIndex = 0;
// Loop through collection to find last category
for (int i = destParent.Nodes.Count - 1; i >= 0; i--)
{
if (destParent.Nodes[i].Tag is Category)
{
newIndex = i + 1;
break;
}
}
destParent.Nodes.Insert(newIndex, selectedNode);
}
selectedNode.Expand();
}
}
else
{
// Switch nodes in same level
tvCategories.BeginUpdate();
_loading = true;
if (selectedNode.Tag is Category)
{
// Only switch category downwards when next node is a catgory
if (selectedNode.NextNode.Tag is Category)
{
// Perform switch
TreeNode switchNode = parent[index + 1];
parent[index + 1].Remove();
parent[index].Remove();
parent.Insert(index, switchNode);
parent.Insert(index + 1, selectedNode);
}
else if (selectedNode.NextNode.Tag is Question)
{
// Make the switch to another node below
if (selectedNode.Parent.NextNode != null)
{
// Parent is always a category
TreeNode categoryParent = selectedNode.Parent.NextNode;
// Remove selected node from current parent
parent.Remove(selectedNode);
// Insert selected node
categoryParent.Nodes.Insert(0, selectedNode);
}
}
}
if (selectedNode.Tag is Question)
{
if (selectedNode.NextNode.Tag is Question)
{
// Perform switch
TreeNode switchNode = parent[index + 1];
parent[index + 1].Remove();
parent[index].Remove();
parent.Insert(index, switchNode);
parent.Insert(index + 1, selectedNode);
}
}
}
tvCategories.EndUpdate();
// Set focus
tvCategories.Focus();
tvCategories.SelectedNode = selectedNode;
SetTreeViewScrollPos(tvCategories, ScrollPos);
「我切換節點」,這是什麼意思? – 2010-07-29 15:59:47
我有一段切換節點的代碼。看到我的編輯在startpost – Martijn 2010-07-30 06:39:06