首先,这是我要做的:我需要动态反转一个堆栈面板中项目的顺序,并在一秒钟内以相反的顺序添加它们。 这是我正在使用的代码:
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
它很好地运行了这段代码,但在用户控件加载之前,我收到了错误。 我环顾四周,似乎有效的解决方案是从我已经做过的第一个集合中删除孩子。 任何帮助将不胜感激。 谢谢
这是我用来解决问题的解决方案,以防其他人遇到这种情况
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