如何在WPF中单击按钮关闭Programaticaly创建的窗口 [英] how to close Programaticaly created window on button click in WPF
本文介绍了如何在WPF中单击按钮关闭Programaticaly创建的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void FormWPF()
{
window2 window = new window2();
#region Button
System.Windows.Controls.Button button1 = new System.Windows.Controls.Button();
button1.Height = 39;
button1.Margin = new System.Windows.Thickness(10, 340, 500, 10);
button1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
button1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
button1.Width = 191;
button1.Content = "Send Email";
button1.Click += new RoutedEventHandler(button1_Click);
Grid.SetRow(button1, 1);
#endregion
newgrid.Children.Add(button1);
}
void button1_Click(object sender, RoutedEventArgs e)
{
// how to close window here this is not code behind of window so I cannot use this.close();
//if i use
//System.Windows.Application.Current.Windows[0].Close();
//throw object null exception
}
推荐答案
您需要有一个要关闭的窗口实例,您可以在打开窗口时创建一个共享类,并放置一个引用对象到您的窗口,然后稍后将其关闭,
但您始终可以针对此问题找到更好的解决方案,
您需要更多地考虑如何组织您的应用,并使之清晰易懂.
you need to have an instance form the window that you want to close you can make a shared class when your window open put a reference object to your window then close it later,
but you always have a better solution for this problem,
you need to think more about how to organize your app and make it clear and ease to understand.
这篇关于如何在WPF中单击按钮关闭Programaticaly创建的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文