WPF 父子窗体间传值
废话不多说,上教程。🤩
WPF 父子窗体间传值
1.父 -> 子
1.1.父窗体.cs
public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); } public void MainWindow_OnLoaded(object sender, RoutedEventArgs e){ ChildrenWindow children= new ChildrenWindow (); children.value1= "父窗体传过来的值!!"; children.sendMessage = Recevie; children.ShowDialog(); } }
1.2.子窗体.cs
public partial class ChildrenWindow : Window{ public string value1 { get; set; } //接受父窗体传过来的值 public ChildrenWindow () { InitializeComponent(); } public void MainWindow_OnLoaded(object sender, RoutedEventArgs e){ TextBox.Text=value1; } }
2.子 -> 父
1.2.子窗体.cs
public partial class ChildrenWindow : Window{ //返回给父窗体 public delegate void SendMessage(string value); public SendMessage sendMessage; public ChildrenWindow () { InitializeComponent(); } public void MainWindow_OnLoaded(object sender, RoutedEventArgs e){ sendMessage("子窗体返回的值!!!");//返回给父窗体 } }
2.2.父窗体.cs
public partial class MainWindow : Window{ //接受子窗体返回来的值 public void Recevie(string value) { ResStr.Text = value; } public MainWindow() { InitializeComponent(); } public void MainWindow_OnLoaded(object sender, RoutedEventArgs e){ ChildrenWindow children= new ChildrenWindow (); children.sendMessage = Recevie; children.ShowDialog(); } }
到这里就🎉🎉🎉大功告成了!🎉🎉🎉 你学废了吗😀
本文来自博客园,作者:浅·笑,转载请注明原文链接:https://www.cnblogs.com/qx-blog/p/18055975
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步