0

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;
}
Michael
  • 2,088
  • 6
  • 29
  • 47

1 Answers1

0

In Back_Click just call UnloadPage.

ShowPage(newPage) already assigned newPage to the the Content property of contentPresenter, therefore the subsequent call to SetCurrentPage fails as the page is already a child of the presenter.

Strillo
  • 2,952
  • 13
  • 15
  • If I just call UnloadPage() in Back_Click on NewPage - it does nothing. If the back button is on MainWindow, it removes it but thats not what I want... I'm not sure why this is. – Michael Jan 26 '12 at 23:05