WPF中窗体切换问题

beginnerboy 2013-03-19 05:05:29
WPF中我定义了两个window窗体,一个是主窗体,一个是子窗体,想要实现在点击主窗体的一个button按钮后打开子窗体,代码如下
            
<Button Content="分组密码测试" HorizontalAlignment="Left" MouseLeftButtonDown="button1_Click" Cursor="Hand" Height="82" Style="{DynamicResource Buttonlist}" VerticalAlignment="Top" Width="78" Margin="401,302,0,0">
<Button.Background>
<ImageBrush ImageSource="skin/zhu/clock256.png" Stretch="Uniform"/>
</Button.Background>
</Button>

以上是主窗体中button按钮的xaml代码
        
private void button1_Click(object sender, MouseButtonEventArgs e)
{
Window1 w2=new Window1();
Application.Current.MainWindow = w2;
this.Hide();
w2.Show();

}

这是在.cs文件中的实现代码。
<Application x:Class="jiemian.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="zhuJM.xaml">

这是Application的代码
但是现在出现问题,无法实现窗体的切换,请问这个问题是处在哪里?
...全文
340 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
三五月儿 2013-03-28
  • 打赏
  • 举报
回复
private void button1_Click(object sender, MouseButtonEventArgs e) { Window1 w2=new Window1(); w2.Owner=this.Window; this.Hide(); w2.Show(); }
xs97 2013-03-28
  • 打赏
  • 举报
回复
Application.Current.MainWindow = w2 应该是这个错了 Window1 w2=new Window1(); //Application.Current.MainWindow = w2; w2.Owner = MainWindow.GetWindow(this); this.Hide(); w2.Show(); 这样就可以了, 关闭子窗体时候 Application.Current.MainWindow.show(); 就切换回主窗体
ariesget 2013-03-20
  • 打赏
  • 举报
回复
引用 7 楼 liuyanwu5289227 的回复:
引用 6 楼 ariesget 的回复:引用 4 楼 liuyanwu5289227 的回复:引用 3 楼 dr592112441 的回复:C# code?1234Window1 w2=new Window1();Application.Current.MainWindow = w2;this.Hide();w2.Show(); 自己窗口都定义错了 你用主窗口打开主……
WPF里的Button这个MouseLeftButtonDown的事件在XAML里这样绑定,如果ClickMode不是Hover的话会被Handler掉。 这个是源码: protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { if (this.ClickMode != ClickMode.Hover) { e.Handled = true; base.Focus(); if (e.ButtonState == MouseButtonState.Pressed) { base.CaptureMouse(); if (base.IsMouseCaptured) { if (e.ButtonState == MouseButtonState.Pressed) { if (!this.IsPressed) { this.SetIsPressed(true); } } else { base.ReleaseMouseCapture(); } } } if (this.ClickMode == ClickMode.Press) { bool flag = true; try { this.OnClick(); flag = false; } finally { if (flag) { this.SetIsPressed(false); base.ReleaseMouseCapture(); } } } } base.OnMouseLeftButtonDown(e); }
famelcn 2013-03-19
  • 打赏
  • 举报
回复
不知道,同问
beginnerboy 2013-03-19
  • 打赏
  • 举报
回复
引用 6 楼 ariesget 的回复:
引用 4 楼 liuyanwu5289227 的回复:引用 3 楼 dr592112441 的回复:C# code?1234Window1 w2=new Window1();Application.Current.MainWindow = w2;this.Hide();w2.Show(); 自己窗口都定义错了 你用主窗口打开主窗口 怎么会有子窗口显示 应该是 Wi……
谢谢,问题解决。能解释一下原因吗?
ariesget 2013-03-19
  • 打赏
  • 举报
回复
引用 4 楼 liuyanwu5289227 的回复:
引用 3 楼 dr592112441 的回复:C# code?1234Window1 w2=new Window1();Application.Current.MainWindow = w2;this.Hide();w2.Show(); 自己窗口都定义错了 你用主窗口打开主窗口 怎么会有子窗口显示 应该是 Window2 w2=new Window2(); 不是……
MouseLeftButtonDown="button1_Click"改成Click="button1_Click"
Mirror然 2013-03-19
  • 打赏
  • 举报
回复
引用 4 楼 liuyanwu5289227 的回复:
引用 3 楼 dr592112441 的回复:C# code?1234Window1 w2=new Window1();Application.Current.MainWindow = w2;this.Hide();w2.Show(); 自己窗口都定义错了 你用主窗口打开主窗口 怎么会有子窗口显示 应该是 Window2 w2=new Window2(); 不是……
那就不知道了 做简单测试是可以通过 要不就是你在控件里面加什么了的
beginnerboy 2013-03-19
  • 打赏
  • 举报
回复
引用 3 楼 dr592112441 的回复:
C# code?1234Window1 w2=new Window1();Application.Current.MainWindow = w2;this.Hide();w2.Show(); 自己窗口都定义错了 你用主窗口打开主窗口 怎么会有子窗口显示 应该是 Window2 w2=new Window2();
不是的,定义的主窗口定义是zhuJM,子窗口定义是Window1,
Mirror然 2013-03-19
  • 打赏
  • 举报
回复

Window1 w2=new Window1();
Application.Current.MainWindow = w2;
this.Hide();
w2.Show();
自己窗口都定义错了 你用主窗口打开主窗口 怎么会有子窗口显示 应该是 Window2 w2=new Window2();
beginnerboy 2013-03-19
  • 打赏
  • 举报
回复
引用 1 楼 ariesget 的回复:
你给的代码我觉得没啥错。你说出问题是指啥?报错内容?
问题是这样的,我是想实现在主窗口点button按钮后,关闭主窗口,打开子窗口,但现在不管怎么点按钮都不管用。没有报错。
ariesget 2013-03-19
  • 打赏
  • 举报
回复
你给的代码我觉得没啥错。你说出问题是指啥?报错内容?

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧