【问题标题】:Specified element is already the logical child of another element. Disconnect it first. After Removal指定元素已经是另一个元素的逻辑子元素。先断开它。拆除后
【发布时间】:2012-11-17 17:41:48
【问题描述】:

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

 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

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

【问题讨论】:

    标签: wpf vb.net


    【解决方案1】:

    这是我用来解决我的问题的解决方案,以防其他人遇到这种情况

      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
    

    【讨论】:

      猜你喜欢
      • 2012-02-20
      • 2012-08-23
      • 1970-01-01
      • 2023-04-05
      • 2019-05-01
      • 2012-06-28
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多