本文介绍了如何在两个wpf窗口之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 标题几乎说明了一切。 我有一个应用程序,将同时打开两个wpf窗口。 我需要一种在这两个窗口之间传递对象的方法。 我已经搜索了几个小时,只是为WinForms提供了解决方案,但没有为wpf windows提供任何内容。 当然这是一项常用的功能,但我无法自己弄清楚或在网上找到示例。 解决方案 CustomEvent并在创建窗口中订阅它。 MainWindow。 xaml.cs 公开 部分 类 MainWindow : 窗口 { 公开 MainWindow () { InitializeComponent (); } 私人 无效 button1_Click ( object sender , RoutedEventArgs e ) { Window1 newWindow = 新 Window1 (); newWindow 。 RaiseCustomEvent + = 新 EventHandler < CustomEventArgs >( newWindow_RaiseCustomEvent ); newWindow 。 显示 (); } 无效 newWindow_RaiseCustomEvent ( object sender , CustomEventArgs e ) { 这个 。 标题 = e 。 消息 ; } } Window1.xaml.cs 公开 部分 类 Window1 : 窗口 { 公开 事件 EventHandler < CustomEventArgs > RaiseCustomEvent ; 公开 Window1 () { InitializeComponent (); } 公开 字符串 myText () { 返回 textBox1 。 文字 ; } 私人 无效 button1_Click ( object sender , RoutedEventArgs e ) { RaiseCustomEvent ( this , new CustomEventArgs ( textBox1 。 Text )); } } 公开 类 CustomEventArgs : EventArgs { 公开 CustomEventArgs ( string s ) { msg = s ; } 私人 字符串 msg ; 公开 字符串 消息 { 得到 { 返回 msg ; } } } The Title pretty much says it all.  I have an application that will have two wpf windows open simultaneously.  I need a way to pass an object between these two windows.  I have searched for a couple of hours, only to be inundated with solutions for WinForms but nothing for wpf windows.  Surely this is a commonly needed capability but I can't figure it out myself or find examples on the web. 解决方案CustomEvent and subscribe to it in the creating window like this.MainWindow.xaml.cspublic partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { Window1 newWindow = new Window1(); newWindow.RaiseCustomEvent += new EventHandler<CustomEventArgs>(newWindow_RaiseCustomEvent); newWindow.Show(); } void newWindow_RaiseCustomEvent(object sender, CustomEventArgs e) { this.Title = e.Message; }}Window1.xaml.cspublic partial class Window1 : Window{ public event EventHandler<CustomEventArgs> RaiseCustomEvent; public Window1() { InitializeComponent(); } public string myText() { return textBox1.Text; } private void button1_Click(object sender, RoutedEventArgs e) { RaiseCustomEvent(this, new CustomEventArgs(textBox1.Text)); }}public class CustomEventArgs : EventArgs{ public CustomEventArgs(string s) { msg = s; } private string msg; public string Message { get { return msg; } }} 这篇关于如何在两个wpf窗口之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 06:39