【WPF】窗体页面间传值总结

2017-06-20 19:27:10来源:CSDN作者:u014234260人点击

以下方法可以根据需求进行变更,

Demo1:子窗体返回值给主窗体

From1

privatevoid button1_Click(object sender, EventArgs e)

{

Form2 frm = newForm2();

//注册事件

frm.TransfEvent += frm_TransfEvent;

frm.ShowDialog();

}

//事件处理方法


void frm_TransfEvent(string value)

{

textBox1.Text = value;

}


Form2:

//声明委托 和 事件

public delegatevoidTransfDelegate(String value);

publiceventTransfDelegate TransfEvent;

privatevoid button1_Click(object sender, EventArgs e)

{

TransfEvent(textBox1.Text);//触发事件

this.Close();

}


===================================================================================================================================

Demo2:主窗体通过触发事件传值给子窗体

Form1:

privatevoid btn_Pass(object sender, RoutedEventArgs e){

 Demo d =newDemo("想要传输的数据");

d.WindowStartupLocation =WindowStartupLocation.CenterScreen;//使窗口位置在最中心、

d.Owner =this;

d.Show();

}

Form2:


public Window2(string str){

InitializeComponent();

show(str);

}

publicvoid show(string s)

{

textbox1.Text = s;

}


=============================================================================================

Demo3:主窗体传输给子窗体(实时)

参考例子:http://www.cnblogs.com/codeToUp/p/5371062.html




Form1

 


privatevoid Button_Click(object sender, RoutedEventArgs e){

Win4 detail = newWin4();

SendMsgEvent += detail.MainFormTxt;//为子窗体注册事件,在子窗体中事件处理代码中设置文本

detail.Owner = this;

detail.Show();

}

privatevoid ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

  SendMsgEvent(this,newClass1() { Text = "传输的值" });

}


Form2

internalvoid MainFormTxtChanedDetailRequest(Object sender, EventArgs e)

{

Class1 arg = e asClass1;//取到主窗体的传来的文本

textbox2.Text= arg.Text;

}


Class1:

publicclassClass1 : EventArgs

{

  publicstring Text { get;set; }

}














最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台