我有一個樹視圖在我通過WCF方法動態構建的SL 4應用程序中。樹視圖由文件夾和文檔組成。當需要添加批量文檔時,我需要能夠重建樹。清除層次結構的silverlight樹視圖
如何清除SL樹視圖中的所有項目?並且不要說TreeView.Items.Clear()
,因爲這不起作用。也不可能使用foreach
循環來移除所有項目及其子項。我說文章here,但在任何地方都看不到TreeView.Children.Copy
或TreeView.Items.Copy
。這是我試過的最後一件事,而且我不知所措...
請讓我知道如果你知道一種方法來完成這件事......謝謝!
Private Sub NewDocumentTree_Received(ByVal sender As Object, ByVal e As GetDocumentTreeCompletedEventArgs)
Me.ThisDocTreeView = e.Result
ClearTree()
PopulateDocTreeView()
End Sub
Private Sub ClearTree()
Dim tempTree As New TreeView()
tempTree = CopyTreeViewToNewObject(Me.docTreeView)
For Each tvi As TreeViewItem In tempTree.Items
For Each sub_tvi As TreeViewItem In tvi.Items
For Each d_tvi As TreeViewItem In sub_tvi.Items
RemoveTreeViewSubItems(d_tvi)
Next
RemoveTreeViewSubItems(sub_tvi)
Next
RemoveTreeViewSubItems(tvi)
Next
tempTree.UpdateLayout()
Me.docTreeView = tempTree
Me.docTreeView.UpdateLayout()
End Sub
Private Sub RemoveTreeViewSubItems(ByVal tvi As TreeViewItem)
For Each sub_tvi As TreeViewItem In tvi.Items
tvi.Items.Remove(sub_tvi)
tvi.UpdateLayout()
Next
End Sub
Private Function CopyTreeViewToNewObject(ByVal treeview As TreeView) As TreeView
Dim newTree As New TreeView()
For Each Parent As TreeViewItem In treeview.Items
newTree.Items.Add(GetTreeViewItemAndChildren(Parent))
Next
Return newTree
End Function
Private Function GetTreeViewItemAndChildren(ByVal treeViewItem As TreeViewItem)
Dim ParentalItem As New TreeViewItem
ParentalItem = treeViewItem
For Each child As TreeViewItem In treeViewItem.Items
Dim firstChild As New TreeViewItem
firstChild = child
For Each subChild As TreeViewItem In child.Items
Dim secondChild As New TreeViewItem
secondChild = child
firstChild.Items.Add(secondChild)
Next
ParentalItem.Items.Add(firstChild)
Next
Return ParentalItem
End Function
UPDATE:下面是從一個批量更新到樹的我的WCF服務,並且構建樹視圖中的主方法接收通知的方法。
Private Sub NewDocumentTree_Received(ByVal sender As Object, ByVal e As GetDocumentTreeCompletedEventArgs)
Me.ThisDocTreeView = e.Result
Me.docTreeView.Items.Clear()
Me.docTreeView.UpdateLayout()
PopulateDocTreeView()
End Sub
Private Sub PopulateDocTreeView()
'ensure all items are clear
Me.docTreeView.Items.Clear()
'loop through root folder to extract parent "user" folders, and examine sub folders
For Each folder As PortalOnline.PortalDocRootFolder In ThisDocTreeView.RootFolders
'get this users folder as a tree view items
Dim parent As TreeViewItem = GetParentTreeViewFolder(folder)
'define a new list of category folders based on this users documents
Dim catFolders As List(Of TreeViewItem) = GetCategoryFolderList(folder)
'loop through the subfolders of the users parent folder for document and get each document folder
'as a new tree view item
For Each child As PortalOnline.PortalDocInfo In folder.SubFolders
'define the document tree view item
Dim childItem As TreeViewItem = GetDocumentTreeItem(child, folder)
'loop through the category folders, and add the document tree view item to the appropriate folder
For Each categoryfolder As TreeViewItem In catFolders
Dim childMeta As DocTreeViewItem = childItem.Tag
'if the category tree veiw item head matches the document category of the document tree view item
'then add the document tree view item into that category folder
If categoryfolder.Header = childMeta.DocumentCategory Then
categoryfolder.Items.Add(childItem)
End If
Next
Next
'add all category folders to the users parent folder
For Each subfolder As TreeViewItem In catFolders
parent.Items.Add(subfolder)
Next
'add the users parent folder to the tree view
docTreeView.Items.Add(parent)
Next
End Sub
在頁面加載時,和NewDocumentTree_Received方法時,WCF發佈和更新被稱爲樹形視圖顯然是被填充。
在實例中的錯誤: 「具有相同鍵的項目已alread增加了」
它告訴我的樹沒有被清除和更新,否則樹就不會在第一填充放置...希望幫助澄清情況。
注意* PopulateTreeView()方法以Me.docTreeView.Items.Clear()開始。 – wakurth 2012-02-14 20:24:21
您能解釋爲什麼'treeView.Items.Clear()'不起作用嗎?它會給出錯誤,不清楚所有項目,或者是什麼?我進行了一次快速測試,似乎對我有用。 – 2012-02-14 22:17:53
當然..當我調用treeView.Items.Clear()絕對沒有任何反應。我也嘗試過treeView.Items.UpdateLayout()之後...仍然沒有。這很簡單。 – wakurth 2012-02-14 22:38:14