C# 如何从 WPF 中的第一个窗口打开第二个窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11133947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I open a second window from the first window in WPF?
提问by ASHOK A
I am new to WPF. I have two windows, such as window1 and window2. I have one button in window1. If I click that button, the window2 has to open. What should I do for that?
我是 WPF 的新手。我有两个窗口,例如 window1 和 window2。我在 window1 中有一个按钮。如果我单击该按钮,则必须打开 window2。我该怎么做?
Here is the code I tried:
这是我试过的代码:
window2.show();
采纳答案by Chandru A
Write your code in window1.
在window1.
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2 = new window2();
win2.Show();
}
回答by Vladislav Zorov
Assuming the second window is defined as public partial class Window2 : Window, you can do it by:
假设第二个窗口定义为public partial class Window2 : Window,您可以通过以下方式实现:
Window2 win2 = new Window2();
win2.Show();
回答by LadislavM
You can create a button in window1 and double click on it. It will create a new click handler, where inside you can write something like this:
您可以在 window1 中创建一个按钮并双击它。它将创建一个新的点击处理程序,您可以在其中编写如下内容:
var window2 = new Window2();
window2.Show();
回答by KF2
private void button1_Click(object sender, RoutedEventArgs e)
{
window2 win2 = new window2();
win2.Show();
}
回答by TokyoMike
When you have created a new WPF application you should have a .xaml file and a .cs file. These represent your main window. Create an additional .xaml file and .cs file to represent your sub window.
创建新的 WPF 应用程序后,您应该有一个 .xaml 文件和一个 .cs 文件。这些代表您的主窗口。创建一个额外的 .xaml 文件和 .cs 文件来表示您的子窗口。
MainWindow.xaml
主窗口.xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Open Window" Click="ButtonClicked" Height="25" HorizontalAlignment="Left" Margin="379,264,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
</Grid>
</Window>
MainWindow.xaml.cs
主窗口.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonClicked(object sender, RoutedEventArgs e)
{
SubWindow subWindow = new SubWindow();
subWindow.Show();
}
}
Then add whatever additional code you need to these classes:
然后将您需要的任何其他代码添加到这些类中:
SubWindow.xaml
SubWindow.xaml.cs
回答by user1399377
In WPF we have a couple of options by using the Show() and ShowDialog() methods.
在 WPF 中,我们通过使用 Show() 和 ShowDialog() 方法有几个选项。
Well, if you want to close the opened window when a new window gets open then you can use the Show() method:
好吧,如果你想在新窗口打开时关闭打开的窗口,那么你可以使用 Show() 方法:
Window1 win1 = new Window1();
win1.Show();
win1.Close();
ShowDialog() also opens a window, but in this case you can not close your previously opened window.
ShowDialog() 也会打开一个窗口,但在这种情况下,您无法关闭之前打开的窗口。
回答by ibmstafa
You can use this code:
您可以使用此代码:
private void OnClickNavigate(object sender, RoutedEventArgs e)
{
NavigatedWindow navigatesWindow = new NavigatedWindow();
navigatesWindow.ShowDialog();
}
回答by Jeff
You will need to create an instance of a new window like so.
您将需要像这样创建一个新窗口的实例。
var window2 = new Window2();
Once you have the instance you can use the Show()or ShowDialog()method depending on what you want to do.
获得实例后,您可以根据需要使用Show()或ShowDialog()方法。
window2.Show();
or
或者
var result = window2.ShowDialog();
ShowDialog() will return a Nullable<bool>if you need that.
Nullable<bool>如果需要,ShowDialog() 将返回一个。
回答by Jeandre Van Dyk
This helped me: The Owner method basically ties the window to another window in case you want extra windows with the same ones.
这对我有帮助: Owner 方法基本上将窗口绑定到另一个窗口,以防您想要额外的窗口具有相同的窗口。
LoadingScreen lc = new LoadingScreen();
lc.Owner = this;
lc.Show();
Consider this as well.
也考虑这一点。
this.WindowState = WindowState.Normal;
this.Activate();

