0

I am trying to pass variable values from One WPF form1 to another WPF form2. Both Forms are open at the same time.

But i did not find any method like Application.OpenForms available for WinForms applications.

Someone referred me this link:

WPF version of Application.OpenForms

Using this link i am able to know open windows only. Is there any way to access value of a variable name age from form1 to form2?

Community
  • 1
  • 1
  • `Application.Current.Windows` – Lei Yang Mar 24 '17 at 00:41
  • There are many, many, many, many articles about how to pass data among forms. There are many possible solutions and it is a matter of **personal preference** how to do it. For example, I would not use a list of open forms or windows or anything like that to pass data among forms for typical requirements. – Sam Hobbs Mar 24 '17 at 01:33

1 Answers1

0

Expose the field that you want to access through a public property:

public partial class Window1 : Window
{
    private int _age;
    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }

    public Window1()
    {
        InitializeComponent();
    }
}

You can then access it from another class like this provided that the window instance has been created and displayed on the screen:

Window1 window = Application.Current.Windows.OfType<Window1>().FirstOrDefault();
if(window != null)
{
    int age = window.Age;
}
mm8
  • 163,881
  • 10
  • 57
  • 88