wpf 子窗体和父窗体的通信

一、如果创建模态对话框: 

form1中 
private void button1_Click(object sender, EventArgs e) 

  Form2 fm = new Form2(); 
  fm.Owner = this;此句在fm.ShowDialog();前面          
  fm.ShowDialog();//创建模态对话框 

form2中 
private void button1_Click(object sender, EventArgs e) 

  Form1 fm = (Form1)this.Owner; 
  TextBox tb = fm.Controls["textBox1"] as TextBox; 
  tb.Text = "ok"; 


二、如果创建非模态对话框,如下: 
form1中 
private void button1_Click(object sender, EventArgs e) 

  Form2 fm = new Form2();          
  fm.Show();//创建非模态对话框,而其这两句部不分次序 
  fm.Owner = this;
 

form2中 
private void button1_Click(object sender, EventArgs e) 

  Form1 fm = (Form1)this.Owner; 
  TextBox tb = fm.Controls["textBox1"] as TextBox; 
  tb.Text = "ok"; 

你可能感兴趣的:(wpf 子窗体和父窗体的通信)