0

Good day, I have an app that I'm busy doing for university. A user enters data on the MainWindow.xaml, I want to make the data that they entered on this window accessible throughout the application (Name, Surname etc. I know that I can parse the data into each individual window but I have 13 Windows total.

Is there a way that I can make a global or session variable in WPF that will allow me to access that data anywhere.

I have looked around for answers but using classes etc seems to be a bad way to go and I cant seem to find a solution that will work.

Thanks in advance.

Nicholas Mott
  • 102
  • 3
  • 13
  • Please create a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) with only the necessary code, otherwise it's difficult to answer. And here's tips on [how to ask](https://stackoverflow.com/help/how-to-ask). – Sach Sep 05 '17 at 22:55
  • 1
    Possible duplicate of [Concrete examples of state sharing between multiple viewmodels (WPF MVVM)](https://stackoverflow.com/questions/4599802/concrete-examples-of-state-sharing-between-multiple-viewmodels-wpf-mvvm) – user247702 Sep 05 '17 at 22:57
  • 2
    Thirteen windows is far too many windows. If your instructor wants you to do it that way, you should do as he says, but bear in mind the very great likelihood that he is criminally insane. Don't sit in the front row, in case he snaps. – 15ee8f99-57ff-4f92-890c-b56153 Sep 05 '17 at 23:04

2 Answers2

3

Is there a way that I can make a global or session variable in WPF that will allow me to access that data anywhere.

Create a class with static properties:

public class ApplicationContext
{
    public static string Name { get; set; }
    public static string Surname { get; set; }
}

You can then access these properties from any other class in your application:

//set:
ApplicationContext.Name = "name...";
//get:
string name = ApplicationContext.Name;

You could also use the Application.Current.Properties collection if you don't want to create your own class for some reason:

//set;
Application.Current.Properties["Name"] = "name";
//get:
string name = Application.Current.Properties["Name"].ToString();
mm8
  • 163,881
  • 10
  • 57
  • 88
1

You can always mark your variables as public static, so they can be accessed from anywhere. You could simply create a static class that holds all your "global" data.

Another option is making the properties of your MainWindow class static but this would result in clustering information onto a class that is actually an interface, not a storage.

Probably the best thing to do is the following:

  • Create a class that is able to hold the data, let's call this class Person with all the variables needed to describe the person (name, surename, etc.)
  • Create a static class that has a public property of the type Person
  • From your MainWindow write your values into the property

There you go, you have a static (global) accessible instance of your data-collection that can be extended and has proper encapsulation and naming.

Something to add: There is no such thing as a "Session" or "Global" variable in C#. You can create classes in global space, but not variables and the word "session" reminds me of a webservice which WPF clearly isn't.

Max Play
  • 3,717
  • 1
  • 22
  • 39