I created a WPF application in c# with 3 different windows, Home.xaml, Name.xaml, Config.xam
l. I want to declare a variable in Home.xaml.cs
that I can use in both the other forms. I tried doing public string wt = "";
but that didn't work.
How can I make it usable by all three forms?
The proper way, especially if you ever want to move to XBAPP, is to store it in
Application.Current.Properties
which is a Dictionary object.
To avoid having to pass around values between windows and usercontrols, or creating a static class to duplicate existing functionality within WPF, you could use:
App.Current.Properties["NameOfProperty"] = 5;
string myProperty = App.Current.Properties["NameOfProperty"];
This was mentioned above, but the syntax was a little off.
This provides global variables within your application, accessible from any code running within it.
You can use a static property:
public static class ConfigClass()
{
public static int MyProperty { get; set; }
}
Edit:
The idea here is create a class that you holds all "common data", typically configurations. Of course, you can use any class but suggest you to use a static class. You can access this property like this:
Console.Write(ConfigClass.MyProperty)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With