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

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

4

1 回答 1

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
于 2012-11-30T16:53:22.240 回答