WPF - 从第一个窗口控制第二个窗口

时间:2010-11-30 18:59:52

标签: c# wpf

WPF - 从Window1我试图最大化以前最小化的Window2。

这就是我打开Window2的方式(我希望使用相同的按钮来最大化它)

 public partial class Window1 : Window
{

    private bool SecondWindowOpen = false;

    public Window1()
    {
        InitializeComponent();
    }

    private void OpenSecondWindow_Click(object sender, RoutedEventArgs e)
    {
        if (SecondWindowOpen == false)
        {
            new Window2().Visibility = Visibility.Visible;
            this.SecondWindowOpen = true;

        }

        else
        {


        }
    }
}

Window2有2个按钮,一个最小化Window2而另一个关闭它。

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

    private void btnMinimize_Click(object sender, RoutedEventArgs e)
    {
        this.WindowState = WindowState.Minimized;
    }

    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }
}

所以打开Window2后,我点击btnMinimize。现在我回到Window1并想要单击OpenSecondWindow并将Window2返回到其原始位置。我认为这是一个语法问题,但我一直找不到任何例子。

谢谢

1 个答案:

答案 0 :(得分:0)

public partial class Window1 : Window
{

    private bool SecondWindowOpen = false;
    private Window2 window2;

    public Window1()
    {
        InitializeComponent();
    }

    private void OpenSecondWindow_Click(object sender, RoutedEventArgs e)
    {
        if (SecondWindowOpen == false)
        {
            window2 = new Window2();
            window2.Visibility = Visibility.Visible;
            this.SecondWindowOpen = true;
        }
        else
        {
            //do whatever you want with window2, like window2.Close();
            //or window2.Visibility = Visibility.Hidden;
        }
    }
}