tags:

views:

1063

answers:

4

I'm a former PHP developer now doing WPF/C# applications, and am having trouble understanding the best way to access global variables and how UserControls access the main Window, etc.

In the constructor of my Window1 class I load UserControls into an internal Dictionary, then dynamically load these into a DockPanel at runtime, which works great and keeps the internal state of each one.

public Window1()
{
    InitializeComponent();

    List<string> userControlKeys = new List<string>();
    userControlKeys.Add("Welcome");
    userControlKeys.Add("News");
    Type type = this.GetType();
    Assembly assembly = type.Assembly;
    foreach (string userControlKey in userControlKeys)
    {
        string userControlFullName = String.Format("{0}.Pages.{1}", type.Namespace, userControlKey);
        UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
        _userControls.Add(userControlKey, userControl);
    }

    //set the default page
    btnWelcome.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}

Question: from within a UserControl how do I access my Dictionary in Window1?

  • I can access the static Class Window1 but not its instantiation hence making a Getter for it gets me no further.
  • there are global attributes in C# but I can imagine there is a better, more OOP way
  • it just seems like I am looking at this in terms of web site / session variables, and thus missing a key concept of desktop development where "state is everywhere"

Hope someone can shed some light on this.

+2  A: 

Try using a singleton object. The idea is to exploit the mechanics of static objects to achieve "global" variables but which are contained in a object for sanity management.

Stuart Childs
A: 

I don't know how it works in WPF, but in WinForms, each control has a FindForm method that returns a reference to its parent form. I would imagine there's something similar in WPF, although I'm still looking for it.

R. Bemrose
there's a static method on Window class called GetWindow that returns a reference to the parent window:Window1 wnd = Window.GetWindow(this) as Window1;
Blend Master
+1  A: 

You have a few options:

  1. The .Net settings object. This brings with it additional features such as serialization. Makes sense if this is some kind of application setting that your entire app should have access too.
  2. You can store is under Window1's Resource collection. Then use the FindResource(...) in the UserControl to find the object. This makes is so that the dictionary is accessible to all element's in Window1's sub tree.
  3. You can leverage UserControl's Tag property. I wouldn't recommend it.
siz
A: 

Once you really get into the Data Binding aspects of WPF, a control should (almost) never need to access its parent in code. Data Binding plus Converters are very powerful. I view control event handlers as an absolute last resort too.

I think you are right; PHP is a very different world. I've been using the MVVM pattern for my WPF and other .Net apps. Take a look at this article about the MVVM pattern and you might get some new ideas: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

AndrewS