0

I have a UserControl userControl1 with button. When user click button i create new UserControl and show it:

private void Button_UserControl1_Click(object sender, RoutedEventArgs e) { Window window = new Window { Title = "Control2", Content = _control2 }; _control2= new UserControl2(ref window); window.ShowDialog(); } 

at UserControl2 when i desides to close myself (control2 window) (another button click):

private void btOk_Click(object sender, RoutedEventArgs e) { _parent.Close(); //_parent is ref window } 

But i can not close it!

Can you tell me: how to close created window (control2) by clicking to button on control2?

Thank you!

1
  • I don't understand how your code is meant to work. What is the value of _control2 at the time you create window? You assign a new UserControl2 to _control2 afterwards, but that won't change the Content of window. Commented Feb 2, 2016 at 8:29

1 Answer 1

2

This should close the parent window:

private void btOk_Click(object sender, RoutedEventArgs e) { Window.GetWindow(this).Close(); } 

This way you don't need to have reference to the parent. GetWindow will always return the window that is hosting the UserControl.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.