如何在两个wpf窗口之间传递数据? [英] How to Pass data between two wpf windows?

查看:713
本文介绍了如何在两个wpf窗口之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎说明了一切。 我有一个应用程序,将同时打开两个wpf窗口。 我需要一种在这两个窗口之间传递对象的方法。 我已经搜索了几个小时,只是为WinForms提供了解决方案
,但没有为wpf windows提供任何内容。 当然这是一项常用的功能,但我无法自己弄清楚或在网上找到示例。


解决方案

< blockquote>


CustomEvent并在创建窗口中订阅它。



MainWindow。 xaml.cs

  公开   部分       MainWindow       窗口  
{
公开 MainWindow ()
{
InitializeComponent ();
}

私人 无效 button1_Click object sender < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336">, RoutedEventArgs e
{
Window1 newWindow = Window1 ();
newWindow
RaiseCustomEvent + = EventHandler < CustomEventArgs >( newWindow_RaiseCustomEvent );
newWindow
显示 ();

}

无效 newWindow_RaiseCustomEvent object sender < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336">, CustomEventArgs e
{
这个 标题 = e 消息 ;
}
}



Window1.xaml.cs

  公开   部分       Window1       窗口  
{
公开 事件 EventHandler < CustomEventArgs > RaiseCustomEvent ;

公开 Window1 ()
{
InitializeComponent ();
}
公开 字符串 myText ()
{
返回 textBox1 文字 ;
}
私人 无效 button1_Click object sender < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336">, RoutedEventArgs e
{

RaiseCustomEvent this < span class ="pln"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336"> new CustomEventArgs textBox1 < span class ="typ"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#2b91af"> Text ));
}
}
公开 CustomEventArgs EventArgs
{
公开 CustomEventArgs string s < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336">)
{
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.cs

public 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.cs

public 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窗口之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆