Vasanth91
Vasanth91

Reputation: 169

Passing data between WPF forms

form1 has a button btnInvoke which invokes form2. form2 contains a textbox and a button btn2.

The user has to enter data in textbox and press btn2.

When btn2 is clicked form2 has to send textbox data to form1.

I have tried passing through constructors but I cant initiate a new instance of form1.

What shall I do?

Upvotes: 3

Views: 33799

Answers (4)

Christoph
Christoph

Reputation: 448

Since you are working with the WPF, use CommandBindings and Messaging. I also recommend you that you take a closser look at MVVM Frameworks, I prevere the MVVM Light Toolkit. There are a lot of HowTos for the framework, just ask google.

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

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

i.e.

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

The second method would be to create a 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; }
    }
}

Upvotes: 10

Daniel Kelley
Daniel Kelley

Reputation: 7737

It may be overkill but an EventAggregator may be a nice solution here. It would allow you to raise an event in form1 that could then be subscribed to from form2.

There are some details and examples of implementing an EventAggregator in https://stackoverflow.com/questions/2343980/event-aggregator-implementation-sample-best-practices.

Upvotes: 1

Habib
Habib

Reputation: 223187

In your form1 define a public property.

public string MyTextData { get; set; }

In your form2 on button click, get the instance of the form1 and set it property to the TextBox value.

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

In your Form1 you will get the text in your property MyTextData

Its better if you following the convention for naming the windows. Use Window instead of Form for naming your windows in WPF. Form is usually used with WinForm applications.

Upvotes: 1

Related Questions