Reputation: 39
I have Main Window into that i have open one user control child1 and have some of textboxes and buttons into that user control.
When the button is clicked at that time I am opening user control name child2 now in user control child2 have on close button and on clicking on that i want to close current user control.
Upvotes: 0
Views: 1051
Reputation: 3237
Assuming you are using the Popup
class to open your UserControl
s
you can close the child1
when child2
is closed as following:
MainWindow.xaml.cs
//Opens the child1 UserControl from MainWindow
private void button_Click(object sender, RoutedEventArgs e)
{
UserControl1 child1 = new UserControl1();
Popup p = new Popup();
child1.ParentPopup = p;
p.Child = child1;
p.IsOpen = true;
}
UserControl1.xaml.cs
public Popup ParentPopup { get; set; }
public UserControl1()
{
InitializeComponent();
}
//Opens the child2 UserControl from child1 UserControl
private void button_Click(object sender, RoutedEventArgs e)
{
UserControl2 child2 = new UserControl2();
Popup p = new Popup();
child2.Unloaded += Child2_Unloaded;
child2.ParentPopup = p;
p.Child = child2;
p.IsOpen = true;
}
//Closes the child1 UserControl when child2 is closed
private void Child2_Unloaded(object sender, RoutedEventArgs e)
{
ParentPopup.IsOpen = false;
}
UserControl2.xaml.cs
public Popup ParentPopup { get; set; }
public UserControl2()
{
InitializeComponent();
}
//Closes the child2 UserControl
private void button_Click(object sender, RoutedEventArgs e)
{
ParentPopup.IsOpen = false;
}
Upvotes: 2