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.UnloadPage();
}

The problem is that the back button on the UserControl, doesn't work unless it's on MainWindow e.g. The UserControl will unload if the back button is on the previous screen e.g. MainWindow in this case - And I can't figure out why this is?

public PageTransition()
{
	InitializeComponent();
}

public void ShowPage(UserControl newPage)
{
	currentPage = newPage;

	if (contentPresenter.Content != null)
	{
		UnloadPage();
	}
	else
	{      
		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;
}

you can do this easily when you click on user control back button just fire your master windows back button.
For this create one public method on your master windows and call that method on your user control back button.

this is my MstaerPage
namespace TestProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
		public void message()
        {
            MessageBox.Show("hi");
        }
		
		private void button2_Click(object sender, EventArgs e)
        {
            UserControl1 u = new UserControl1();
            this.Controls.Add(u);
        }
	}
}

this is my usercontrol
namespace TestProject
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            f.message();
        }
    }
}

//when i click on user controls button it will show me the message of master page.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.