title: Beginner - confused about binding and resources in WPF
body: <p>I'm trying to learn WPF but I find it very difficult to understand bindings, the "resources" thing, and object creation.
  My background is in C++/MFC and C#-Winforms.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li><p>Most of the examples I see in XAML (in MSDN and in two other WPF books I've read) use StaticResource in the binding expression.  Are these related in any way to static members?  Or is this just a misleading name?  When a reference is made to any object as a StaticResource, when is it instantiated?</p></li>
  <li><p>As far as I can see StaticResources are used with "things" defined in the "Resources" section of the app/window/control etc.<br>
  Now, these Resources sections are very confusing to me.
  What exactly are they?  From my experience in MFC these were icons, strings, etc.  However, judging by all the examples I've seen, in WPF these seem to be essentially a "dumping ground" for 
  <b>(a)</b> all kinds of global object definitions in markup (styles, data templates, etc)
  <b>(b)</b> all kinds of global object instantiations in markup
  Am I correct?  This strikes me as very messy.<br>
  It essentially involves learning all sorts of semi-DSLs in XAML (for defining styles, for defining data templates, for creating objects etc), and sticking them together in the same place.
  I keep thinking about something like editing the resource file (.rc) in MFC by hand.  At least there the sections were well separated and the syntax for each resource was relatively simple.</p></li>
  <li><p>To tie up the previous two questions:  When I define an object instance in the Resources section, and later reference it from a StaticResource binding, when exactly is it instantiated?
  MSDN says (in "How to: Make Data Available for Binding in XAML"):  </p></li>
  </ol>
  
  <blockquote>
    <p>one way you can make the object
    available for binding is to define it
    as a resource</p>
  </blockquote>
  
  <p>However, this isn't very clear.  What do they mean available? Do they mean created? Do they mean hooked up to the binding subsystem?  And when exactly is that object <strong>created</strong>?
  From playing around with a simple example I saw that WPF seems to create this object for me when it tries to attach the binding.  And this is even more confusing.</p>
  
  <p>EDIT:
  After the clarification by karmicpuppet below, I'm still confused as to how this is connected to Binding.
  Suppose I have in my resources: </p>
  
  <pre><code>&lt;local:Person x:Key="MyPerson" Name="Title"/&gt; 
  </code></pre>
  
  <p>(where Person is a class with a property called Name) and then in the window I have: </p>
  
  <pre><code>&lt;TextBlock Text="{Binding Source={StaticResource MyPerson}, Path=Name}"/&gt; 
  </code></pre>
  
  <p>1) What does this do? Does it goes through the same steps - searching for the resource and then applying it to the Text property? Does the MyPerson object gets created at the time of Window creation, or later?
  2) Do I have to use the Binding mechanism to bind to the Name property? Can't I bind to it directly like you did above with myBrush?  Why can't I do something like this?  </p>
  
  <pre><code>&lt;TextBlock Text="{StaticResource MyPerson, Path=Name}"/&gt; 
  </code></pre>
  
  <p>Is it just a short-sightedness on the part of the framework?  I think I'm missing very big here, but I can't seem to understand what...</p>
  
  <p>3) I tried using DynamicResource, but I am very confused about each step I took.
   a) Added a DependencyObject with a DependencyProperty above my single Window class in code (is this DependencyObject necessary?)</p>
  
  <pre><code>public class SomeText : DependencyObject
  {
      public string Header
      {
          get { return (string)GetValue(HeaderProperty); }
          set { SetValue(HeaderProperty, value); }
      }
      public static readonly DependencyProperty HeaderProperty =
          DependencyProperty.Register("Header", typeof(string), typeof(SomeText), new UIPropertyMetadata(0));
  }
  </code></pre>
  
  <p>b) Added an instance of it to the Windows.Resources (is this necessary with DynamicResource?  MSDN seems to say no, but if so I can't figure out how to do the next step in XAML)</p>
  
  <p></p>
  
  <p>c) I tried both:</p>
  
  <pre><code>Text="{Binding Source={DynamicResource HeaderText}, Path=Header}"
  </code></pre>
  
  <p>Which gave me an exception, and</p>
  
  <pre><code>Text="{DynamicResource HeaderText}"
  </code></pre>
  
  <p>But I couldn't understand where to put the path to the Header property.</p>
  
  <p>This is my 5th or so attempt to fiddle around with WPF lately, and each time I get stumped by this seemingly simple things which don't work.  I've read 2 books and I really try to understand the MSDN articles, however they're of no help at all.  </p>