在wpf应用程序中,隐藏窗口,然后在从另一个窗口

时间:2016-08-19 21:27:19

标签: c# wpf

以下是它应该如何运作:

Window1:单击按钮可打开一个新窗口并隐藏当前窗口 (此页面包含一些文本框)

public void applyBtn_Click(object sender, EventArgs e)
        {
...
    DataDisplay dd = new DataDisplay();
  dd.Visibility = Visibility.Visible;
...
}

新窗口:单击按钮可以从相同的状态打开Window1(所有复选框,文本框应包含关闭时的值)

  private void settings_btn_Click(object sender, RoutedEventArgs e)
        {
            Filters filter = new Filters();
            filter.Visibility = Visibility.Visible;
        }
//As you can see I am calling a new instance instead of reopening the same hidden instance.

我不是每次都创建一个新实例,而是每次都使用相同的实例。如果还有其他任何不那么复杂的方法,请随意拍摄!

1 个答案:

答案 0 :(得分:0)

如果我理解正确,我认为最好让“MainWindow”管理2个更改窗口您可以在2个Windows事件或使用代理处理它:

public partial class MainWindow : Window
{
    static frmFilters filterWindowInstance = new frmFilters(OpenDataDisplay);
    static frmDataDisplay dataWindowInstance = new frmDataDisplay(OpenFilters);

    static void OpenFilters()
    {
        filterWindowInstance.Visibility = Visibility.Visible;
        dataWindowInstance.Visibility = Visibility.Collapsed;
    }

    static void OpenDataDisplay()
    {
        filterWindowInstance.Visibility = Visibility.Collapsed;
        dataWindowInstance.Visibility = Visibility.Visible;
    }

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        OpenFilters();

        Visibility = Visibility.Collapsed;
    }

}

public partial class frmFilters : Window
{
    Action CloseFilterOpenData;

    public frmFilters(Action CloseFilterOpenDataToSet)
    {
        InitializeComponent();

        CloseFilterOpenData = CloseFilterOpenDataToSet;
    }

    private void cmdHideThis_Click(object sender, RoutedEventArgs e)
    {
        CloseFilterOpenData();
    }
}

public partial class frmDataDisplay : Window
{
    Action CloseDataOpenFilter;

    public frmDataDisplay(Action CloseDataOpenFilterToSet)
    {
        InitializeComponent();

        CloseDataOpenFilter = CloseDataOpenFilterToSet;
    }

    private void cmdHideThis_Click(object sender, RoutedEventArgs e)
    {
        CloseDataOpenFilter();
    }
}