我使用this站點的示例來創建MultiColumnStackPanel。WP7自定義MultiColumnStackPanel項目子項已具有父項
然而有一個問題。一旦頁面被加載,堆疊面板看起來很好,就像這個例子一樣。但是,當我導航到下一頁,然後返回到上一頁時,我收到一個異常:
InvalidOperationException隨着以下消息:
Element is already the child of another element.
。
所以我認爲包含在原來的自定義堆棧面板中的項目被添加到一個新的自定義堆棧面板。但爲什麼MultiColumnStackPanel正在'刷新'並且是否仍然存在MultiColumnStackPanel.Items?
是否有其他人對此問題有解釋/解決方案?
在此先感謝
。
MultiColumnStackPanel類:
public class MultiColumnStackPanel : StackPanel
{
public int NumberOfColumns { get; set; }
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items",
typeof(Collection<UIElement>),
typeof(MultiColumnStackPanel),
new PropertyMetadata(new Collection<UIElement>()));
public Collection<UIElement> Items
{
get { return (Collection<UIElement>)GetValue(ItemsProperty); }
}
public MultiColumnStackPanel()
{
Loaded += (s, e) =>
{
LoadItems();
};
}
void LoadItems()
{
Children.Clear();
if (Items != null)
{
StackPanel sp = CreateNewStackPanel();
foreach (UIElement item in Items)
{
sp.Children.Add(item);
if (sp.Children.Count == NumberOfColumns)
{
Children.Add(sp);
sp = CreateNewStackPanel();
}
}
if (sp.Children.Count > 0)
Children.Add(sp);
}
}
private StackPanel CreateNewStackPanel()
{
Orientation oppositeOrientation;
if(this.Orientation.Equals(Orientation.Vertical))
oppositeOrientation = Orientation.Horizontal;
else
oppositeOrientation = Orientation.Vertical;
return new StackPanel() { Orientation = oppositeOrientation };
}
}
XAML例如:
<model:MultiColumnStackPanel x:Name="customStackPanel" Orientation="Horizontal" NumberOfColumns="2">
<model:MultiColumnStackPanel.Items>
<Rectangle Height="80" Width="80" Margin="10" Fill="Green" />
<Rectangle Height="80" Width="80" Margin="10" Fill="Green" />
<Rectangle Height="80" Width="80" Margin="10" Fill="Green" />
<Rectangle Height="80" Width="80" Margin="10" Fill="Green" />
</model:MultiColumnStackPanel.Items>
</model:MultiColumnStackPanel>
仔細調試應用程序的步驟步。當返回時,Loaded事件再次被提出,我想那會導致YOu的問題。 – nkchandra
感謝提示 – akalucas