Ok so I'm making a WPF menu system and the problem is as follows:
I have a MainWindow which has a button that triggers a transition to the next UserControl:
private void Button_Click(object sender, RoutedEventArgs e)
{
NewPage newPage = new NewPage();
pageTransitionControl.ShowPage(newPage);
}
On this UserControl (NewPage), it has a back button which should transition the current UserControl away, so it goes back to MainWindow.
private void Back_Click(object sender, RoutedEventArgs e)
{
pageTransitionControl.SetCurrentPage(newPage);
pageTransitionControl.UnloadPage();
}
The problem lies in the SetCurrentPage(UserControl uc) - it tells me that the "Specified element is already the logical child of another element. Disconnect it first." - I'm not sure what that means in this context/how to fix this?
void ShowNextPage()
{
currentPage.Loaded += newPage_Loaded;
contentPresenter.Content = currentPage;
}
public void UnloadPage()
{
Storyboard hidePage = (Resources[string.Format("{0}Out", TransitionType.ToString())] as Storyboard).Clone();
hidePage.Completed += hidePage_Completed;
hidePage.Begin(contentPresenter);
}
void newPage_Loaded(object sender, RoutedEventArgs e)
{
Storyboard showNewPage = Resources[string.Format("{0}In", TransitionType.ToString())] as Storyboard;
showNewPage.Begin(contentPresenter);
}
void hidePage_Completed(object sender, EventArgs e)
{
contentPresenter.Content = null;
}
public void SetCurrentPage(UserControl uc)
{
contentPresenter.Content = uc;
}