wpf 点击按钮弹出新对话框,如何通过单击按钮(wpf)来打开和关闭窗口?

Hi friends,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();

}

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.

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,.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值