WPF中窗体切换问题
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的代码
但是现在出现问题,无法实现窗体的切换,请问这个问题是处在哪里? --------------------编程问答-------------------- 你给的代码我觉得没啥错。你说出问题是指啥?报错内容? --------------------编程问答--------------------
问题是这样的,我是想实现在主窗口点button按钮后,关闭主窗口,打开子窗口,但现在不管怎么点按钮都不管用。没有报错。 --------------------编程问答--------------------
Window1 w2=new Window1();
Application.Current.MainWindow = w2;
this.Hide();
w2.Show();
自己窗口都定义错了 你用主窗口打开主窗口 怎么会有子窗口显示
应该是 Window2 w2=new Window2(); --------------------编程问答--------------------
不是的,定义的主窗口定义是zhuJM,子窗口定义是Window1, --------------------编程问答--------------------
那就不知道了 做简单测试是可以通过
要不就是你在控件里面加什么了的 --------------------编程问答--------------------
MouseLeftButtonDown="button1_Click"改成Click="button1_Click" --------------------编程问答--------------------
谢谢,问题解决。能解释一下原因吗? --------------------编程问答-------------------- 不知道,同问 --------------------编程问答--------------------
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);
}
--------------------编程问答-------------------- 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();
就切换回主窗体 --------------------编程问答-------------------- private void button1_Click(object sender, MouseButtonEventArgs e)
{
Window1 w2=new Window1();
w2.Owner=this.Window;
this.Hide();
w2.Show();
}
补充:.NET技术 , C#