Friday, October 21, 2016

Passing data between WPF forms

Method 1: Pass the data into the construction or set the data through its public method:

Window2 newWindow = new Window2(strForm1Text);

newWindow.TestMsg = strForm1Text;

Method 2: You can set the First window as the Second window's DataContext

namespace Banking_System
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSwitchForm_Click(object sender, RoutedEventArgs e)
        {
            Window2 newWindow = new Window2(strForm1Text);
            newWindow.DataContext = this;
            newWindow.ShowDialog();
        }
    }

    public partial class MainWindow2 : Window
    {
        public MainWindow2()
        {
            var window1 = this.DataContext;
        }
    }
}

Method 3: using ShowDialog and a public method then testing that the DialogResult is true then reading the value from the method.

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

Method 4: Create an event in your second window, have the parameters of the event's delegate contain whatever information you want to pass:

public class Popup : Window
{
    public event Action<string> Check;

    public void Foo()
    {
        //fire the event
        if (Check != null)
            Check("hello world");
    }
}

Then the main window can subscribe to that event to do what it wants with the information:

public class Main : Window
{
    private Label label;
    public void Foo()
    {
        Popup popup = new Popup();

        // use the Action<T> delegate with anonymous methods:
        popup.Check += delegate (string s)
        {
            label.Content = s;
        };

        // this is the shorthand of the above:
        popup.Check += value => label.Content = value;

        popup.ShowDialog();
    }
}

Method 5: 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; }
    }
}

Method 6:

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.

Reference:

http://stackoverflow.com/questions/30063550/how-should-i-pass-data-between-wpf-windows-involving-mainwindow-c

http://stackoverflow.com/questions/14433935/passing-data-between-wpf-forms

http://stackoverflow.com/questions/21607925/c-sharp-return-variable-from-child-window-to-parent-window-in-wpf

http://stackoverflow.com/questions/2282476/actiont-vs-delegate-event

No comments: