Passing Data Between WPF Forms

form1

has a button btnInvoke

that calls form2

. form2

contains a textbox

and a button btn2

.

The user has to enter data in textbox

and click btn2

.

When clicked, you btn2

form2

need to send textbox data

to form1

.

I have tried walking through constructors, but I cannot initiate a new instance form1

.

What should I do?

+3


source to share


4 answers


There are two methods you can use. The first one would be using ShowDialog and a public method, then checking that the DialogResult is true and then reading the value from the method.

i.e.

if (newWindow.ShowDialog() == true)
            this.Title = newWindow.myText();

      

The second way is to create a CustomEvent and subscribe to it in the create 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; }
    }
}

      

+10


source


In yours, form1

specify a public property.

public string MyTextData { get; set; }

      

In your form2

on click , get an instance form1

and set its property to the TextBox value.



var frm1 = Application.Current.Windows["form1"] as Form1;
if(frm1 ! = null)
    frm1.MyTextData  = yourTextBox.Text;

      

In yours form1

you will get the text in the propertyMyTextData

It's better if you follow the window naming convention. Use Window

instead Form

to label your windows in WPF. Form is commonly used with WinForm applications.

+1


source


This might be too much, but it EventAggregator

might be a good solution. This will allow you to raise the event to form1

, which can then be subscribed to form2

.

There are several details and implementation examples EventAggregator

at https://stackoverflow.com/questions/2343980/event-aggregator-implementation-sample-best-practices .

+1


source


Since you are working with WPF, use CommandBindings and Messaging . I also recommend that you take a look at the MVVM constructor, I am reading the MVVM Light Toolkit . There are many principles for the platform, just ask Google.

0


source







All Articles