如果在辅助窗口关闭或关闭时启用主窗口上的按钮? - WPF VB.NET

时间:2016-02-26 13:15:26

标签: wpf vb.net

我有两个窗户。主窗口&窗口1。

在主窗口上,有一个按钮1。单击它时,它将被禁用并打开Window1。但我想在Window1关闭或关闭时在主窗口上启用button1。

3 个答案:

答案 0 :(得分:0)

我猜你使用的是WinForms。在这种情况下,您有一个用于单击button1的事件处理程序:

private void OnButton1Clicked(object sender, ...)
{
    // show window 1
}

现在有两种方法可以显示表单。您可以将其显示为无模式对话框或模式对话框。

  • 模态对话框,要求用户在继续执行程序之前做出响应
  • 无模式对话框,保留在屏幕上,随时可供使用,但允许其他用户活动

您看到的大多数对话框都是模态:如果按文件保存,则必须先完成“保存文件”对话框,然后才能继续编辑。

模态对话框最简单   - 使用Form.ShowDialog显示它们。   - 表单关闭时ShowDialog返回。

如果使用模态对话框,则代码看起来是连续的:

private void OnButton1Clicked(object sender, ...)
{
    using (Window1 window1 = new Window1())
    {
        // if needed window1.SetValues...
        var dlgResult = window1.ShowDialog(this);
        // if here, window 1 is closed
        if (dlgResult = DialogResult.OK)
        {   // ok button pressed
            // if needed: window1 read resulting values
        }
     }  // because of using window 1 automatically disposed
}

但是,如果window1显示为无模式对话框,则window1必须告诉其他人它已关闭。使用事件Form.Closed:

private Window1 window1 = null;
private void OnButton1Clicked(object sender, ...)
{
   if (this.window1 != null) return; // window1 already shown

   this.window1 = new Window1())
   this.window1.Closing += this.OnFormClosed;
}

private void OnFormClosed(object sender, FormClosedEventArgs e)
{
    Debug.WriteLine("window1 closed");
    if (this.window1.DialogResult = DialogResult.OK)
    {
        // process dialog results
    }
    this.window1.Dispose();
    this.window1 = null;
}

答案 1 :(得分:0)

在Window1中创建一个公共按钮

public Button mainBtn ; 
在按钮点击事件

上的mainWindow上

private void button_click(object sender , RoutedEventArgs e){
Window1 win = new Window1();
this.button.IsEnabled = false; 
win.mainBtn = this.button;
win.Show();
}

将关闭事件添加到Window1

   private void Window_closing(object sender , CancelEventArgs e){
    mainBtn.IsEnabled = true; 
}

的想法是将MainWindow按钮传递给Window1按钮 然后你可以像你想要的那样控制它。

答案 2 :(得分:0)

数据绑定是WPF中最强大的工具: 添加按钮并将IsEnabled属性绑定到视图模型或后面的代码中的公共属性。在辅助窗口中 - 关闭时 - 更新属性以反映新状态。

不要忘记实施INotifyPropertyChanged

相关问题