如何通过单击一个按钮(wpf)来编码打开和关闭窗口的代码? [英] how to code open and close a window in single click a button (wpf)?

查看:412
本文介绍了如何通过单击一个按钮(wpf)来编码打开和关闭窗口的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

private void btn1_Click(object sender, RoutedEventArgs e)
{

Window2 win2 = new Window2();
win2.ShowDialog();
this.close();

}



它不是关闭的窗口.,所以我修改了代码,..



it is not close window., so i modify the code,..

private void btn1_Click(object sender, RoutedEventArgs e)
{
Window2 win2 = new Window2();
// win2.Owner = this;
win2.Show();
 this.Close();
}



现在,该窗口已关闭,但是... window2显示为空.它没有显示任何按钮或标签.如果最小化和最大化窗口,则仅显示按钮.

我能做些什么?. :rolleyes:



Now the window is close but,... the window2 is show empty,. it didn''t show any button or labels. if minimize and maximize the window only it show the buttons,.

What can i do?. :rolleyes:

推荐答案

好,我无法重现您的问题.我在主要形式的按钮单击处理程序中执行此操作:

Ok, I could not reproduce your problem. I do this in a button click handler on the main form:

private void button1_Click(object sender, RoutedEventArgs e)
{
    Child child = new Child();
    child.Show();
    this.Close();
}



子窗口正确显示(根本没有绘画问题).因此,您可能正在代码中执行其他操作,从而导致问题.



The child window shows up correctly (no painting issues at all). So you may be doing something else in your code that''s causing problems.


第一个代码示例应显示对话框,但仅在对话框关闭后才关闭this窗口(因为它是模态的.

第二个示例应按预期方式工作.应该显示新创建的窗口.

它最初不显示其内容,它可能由于与Window2的设计有关的各种原因而发生;一个典型的原因是布局不正确.尝试调用其InvalidateArrange和/或UpdateLayout.如果有帮助,这还不是解决方案.检查过载的OnContentRenderedOnRender是任意的.这也可能是窗口调整大小事件上行为不正确的结果.仅用于测试,make是可调整大小的(如果不是),并在手动调整大小时检查它是否不会弄乱布局.

—SA
First code sample should show dialog but not close the this window only after dialog is closed (because it is modal).

The second sample should work as expected. The newly created Window should be shown.

It it does not show its content at first, it can happen by different reasons related to design of Window2; one typical reason is incorrect layout. Try to call its InvalidateArrange and/or UpdateLayout. If it helps, this is not a solution yet. Check up overloaded OnContentRendered, OnRender is any. It also can be a result of incorrect behavior on window resize events. just for tests, make is resizeable (if it is not) and check if it does not screw up layout when you manually resize it.

—SA


我无法关闭一个文件.因此,请关闭该方法以外的所有内容.

这不是解决这个问题的正确方法.但这解决了.

我可以在我们的代码项目此处找到该代码.

我希望此代码也不会出现问题,..

I am unable to close one file,. so switch to close all but this method,.

its not a correct sollution of this problem,. but it solves that,.

i find this code in our Code project here.

I hope feature also this code has not give problems,..

private void CloseAllWindows()
{
for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
App.Current.Windows[intCounter].Close();
}

private void btn1_Click(object sender, RoutedEventArgs e)
  {
   CloseAllWindows();
   Window2 win2 = new Window2();
   // win2.Owner = this;
   win2.Show();
   //this.Close();
   }



谢谢大家.



Thank to all,.


这篇关于如何通过单击一个按钮(wpf)来编码打开和关闭窗口的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆