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;
}

Dani AI

Generated

The symptom here is that the back button in the UserControl is calling a different instance (or no instance) of the PageTransition than the one actually hosting the control. That explains why the same call works when the button is on MainWindow but not when it runs from NewPage. , the UserControl does not automatically have access to the hosting control instance unless you explicitly wire it up. s sample confirms the common mistake: creating a new MainWindow/Form inside the UserControl will not affect the live UI — it just makes a separate object.

A simple, robust fix is to raise a named event from the UserControl and let the host (MainWindow) handle it. This keeps the child decoupled and avoids walking the visual tree:

public event EventHandler RequestBack;

private void Back_Click(object sender, RoutedEventArgs e)
{
    RequestBack?.Invoke(this, EventArgs.Empty);
}

When creating and showing the page in MainWindow, attach a handler that calls your transition control:

var newPage = new NewPage();
newPage.RequestBack += (s, e) => pageTransitionControl.UnloadPage();
pageTransitionControl.ShowPage(newPage);

Alternative approaches: expose a public method on the host (do not construct a new host inside the UserControl) and find the host at runtime via Window.GetWindow(this) or a VisualTree helper, or use ICommand/DataContext binding with a shared ViewModel (MVVM). Troubleshooting tips: place a breakpoint in the UserControl back handler to inspect what object pageTransitionControl refers to (null? new instance?), and avoid attaching long-lived event handlers on dynamic pages without unsubscribing to prevent memory leaks.

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.