如何在模式对话框中设置主窗口中的变量

时间:2012-03-07 00:06:14

标签: c# wpf

我很抱歉我的愚蠢问题,但我被困了。我在创建应用程序,它将使用SQL。在MainWindow中,我有一个显示SQL查询结果的DataGrid。我想在模态对话框中设置查询。我创建了它,但我不知道如何将模式对话框中TextBox的字符串连接到主窗口中的字符串。

从此窗口分配字符串查询:

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

        private void ask_DB_Click(object sender, RoutedEventArgs e)
        {
            string query = textBox1.Text();
        }
.....
}

到主窗口

public partial class MainWindow : Window
{

    string DB_query = DB_conn_win.query;


    public MainWindow()
    {
        InitializeComponent();
 .....

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

public partial class DB_conn_win : Window 
{ 

    private void ask_DB_Click(object sender, RoutedEventArgs e) 
    { 
        this.Query = textBox1.Text(); 
    } 
    public string Query;
..... 
} 



public partial class MainWindow : Window 
{ 

  string DB_query = DB_conn_win.query; 

  public SomeButton_Click(object sender, RoutedEventArgs e)
  {
     var dialog = new DB_conn_win();
     if (dialog.ShowDialog() == true)
     {
       this.DB_query = dialog.Query;
     }
  }

答案 1 :(得分:0)

public partial class DB_conn_win : Window { 
    public string query;
    private void ask_DB_Click(object sender, RoutedEventArgs e) { 
        this.query = textBox1.Text(); 
    } 
}

public partial class MainWindow : Window {
    string DB_query;

    public MainWindow() {
        InitializeComponent();
        Loaded += Window_Loaded;
    }

    void Window_Loaded(object sender, RoutedEventArgs e) {
        DB_conn_win dialog = new DB_conn_win();
        dialog.Owner = this;
        dialog.ShowDialog();
        if (dialog.DialogResult != null && dialog.DialogResult.Value)
            DB_Query = dialog.query;
     }   
}

ShowDialog和DialogResult检查取决于对话框的设置方式 - 有关详细信息,请参阅this link