指定的元素已经是另一个元素的逻辑子元素。首先断开它。搬家后

时间:2012-11-29 15:43:27

标签: wpf vb.net

首先,我要做的是:我需要在一个堆栈面板中动态反转项目的顺序,并在一秒钟内以相反的顺序添加它们。这是我正在使用的代码:

 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

它运行此代码很好,但在用户控件加载之前我收到错误。我在这里环顾四周,似乎有效的解决方案是将孩子从我已经做过的第一个收藏中删除。任何帮助,将不胜感激。谢谢

1 个答案:

答案 0 :(得分: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