2012-11-29 54 views
0

首先,我要做的是:我需要動態地顛倒一個堆棧面板中項目的順序,並在一秒內以相反順序添加它們。這裏是我使用的代碼:指定的元素已經是另一個元素的邏輯子元素。先斷開它。刪除後

Dim cp As ContentPresenter = TryCast(Utilities.GetDescendantFromName(TitleLegend, "ContentPresenter"), ContentPresenter) 
     If cp IsNot Nothing Then 
      Dim sp As StackPanel = TryCast(cp.Content, StackPanel) 
      If sp IsNot Nothing Then 
       Dim nsp As New StackPanel() With {.Orientation = System.Windows.Controls.Orientation.Vertical} 
       Dim count As Integer = sp.Children.Count 
       For i As Integer = count - 1 To 0 Step -1 
        Dim cc As ContentControl = TryCast(sp.Children(i), ContentControl) 
        If cc IsNot Nothing Then 
         sp.Children.Remove(cc) 
         nsp.Children.Add(cc) 
        End If 
       Next 
       cp.Content = Nothing 
       cp.Content = nsp 
      End If 
     End If 

它運行通過此代碼很好,但就在用戶控制加載之前我收到錯誤。我在這裏環顧四周,似乎工作的解決方案是從我已經做的第一個集合中刪除孩子。任何幫助,將不勝感激。謝謝

回答

0

這是我用來解決我萬一別人問題的解決方案運行到這種情況

Dim cp As ContentPresenter = TryCast(Utilities.GetDescendantFromName(TitleLegend, "ContentPresenter"), ContentPresenter) 
     If cp IsNot Nothing Then 
      Dim sp As StackPanel = TryCast(cp.Content, StackPanel) 

      If sp IsNot Nothing Then 
       If tempList Is Nothing Then 
        tempList = New List(Of ContentControl) 
       End If 
       Dispatcher.BeginInvoke(New Action(Function() 
                 Dim count As Integer = sp.Children.Count 
                 For i As Integer = count - 1 To 0 Step -1 
                  Dim cc As ContentControl = TryCast(sp.Children(i), ContentControl) 
                  sp.Children.Remove(TryCast(sp.Children(i), ContentControl)) 
                  If cc IsNot Nothing Then 
                   tempList.Add(cc) 
                  End If 
                 Next 

                 For i As Integer = count - 1 To 0 Step -1 
                  Dim cc As ContentControl = TryCast(tempList(0), ContentControl) 
                  tempList.Remove(TryCast(tempList(0), ContentControl)) 
                  If cc IsNot Nothing Then 
                   sp.Children.Add(cc) 
                  End If 
                 Next 

                End Function), System.Windows.Threading.DispatcherPriority.Background, Nothing) 
      End If 
     End If 
相關問題