2012-11-29 2 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 
로 실행
관련 문제