Programming

WPF의 첫 번째 창에서 두 번째 창을 어떻게 열 수 있습니까?

procodes 2020. 7. 26. 13:50
반응형

WPF의 첫 번째 창에서 두 번째 창을 어떻게 열 수 있습니까?


WPF를 처음 사용합니다. window1과 window2와 같은 두 개의 창이 있습니다. window1에 하나의 버튼이 있습니다. 해당 버튼을 클릭하면 window2가 열려 있어야합니다. 이를 위해 어떻게해야합니까?

내가 시도한 코드는 다음과 같습니다.

window2.show();

에 코드를 작성하십시오 window1.

private void Button_Click(object sender, RoutedEventArgs e)
{
    window2 win2 = new window2();
    win2.Show();
    this.Close();
}

새로운 WPF 응용 프로그램을 만들고 싶을 것입니다. 이 작업을 완료하면 .xaml 파일과 .cs 파일이 있어야합니다. 기본 창을 나타냅니다. 하위 창을 나타내는 추가 .xaml 파일과 .cs 파일을 만듭니다.

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

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ButtonClicked(object sender, RoutedEventArgs e)
    {
        SubWindow subWindow = new SubWindow();
        subWindow.Show();
    }
}

그런 다음이 클래스에 필요한 추가 코드를 추가하십시오.

SubWindow.xaml
SubWindow.xaml.cs

private void button1_Click(object sender, RoutedEventArgs e)
{
    window2 win2 = new window2();
    win2.Show();
}

두 번째 창이로 정의되었다고 가정하면 다음과 같이 public partial class Window2 : Window할 수 있습니다.

Window2 win2 = new Window2();
win2.Show();

이것은 나에게 도움이되었다 : Owner 메소드는 기본적으로 같은 창을 가진 추가 창을 원할 경우 창을 다른 창에 연결합니다.

LoadingScreen lc = new LoadingScreen();
lc.Owner = this;
lc.Show();

이것도 고려하십시오.

this.WindowState = WindowState.Normal;
this.Activate();

In WPF we have a couple of options by using the Show() and ShowDialog() methods.

Well, if you want to close the opened window when a new window gets open then you can use the Show() method:

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.


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.

window2.Show();

or

var result = window2.ShowDialog();

ShowDialog() will return a Nullable<bool> if you need that.


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:

var window2 = new Window2();
window2.Show();

You can use this code:

private void OnClickNavigate(object sender, RoutedEventArgs e)
{
    NavigatedWindow navigatesWindow = new NavigatedWindow();
    navigatesWindow.ShowDialog();
}

참고URL : https://stackoverflow.com/questions/11133947/how-do-i-open-a-second-window-from-the-first-window-in-wpf

반응형