Alex Rivera | Logout

Beginner - confused about binding and resources in WPF

Asked 2010-08-28T16:01:07.523
31

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.

My questions:

  1. 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?

  2. As far as I can see StaticResources are used with "things" defined in the "Resources" section of the app/window/control etc.
    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 (a) all kinds of global object definitions in markup (styles, data templates, etc) (b) all kinds of global object instantiations in markup Am I correct? This strikes me as very messy.
    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.

  3. 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"):

one way you can make the object available for binding is to define it as a resource

However, this isn't very clear. What do they mean available? Do they mean created? Do they mean hooked up

Edit
Report

1 Answer

44

First, an overall comment:

WPF is hard to learn. It's hard to learn because there are several different fundamentally new concepts that you have to get your head around at the same time. The struggle that you're having right now is that you're trying to learn at least three different things at once:

  • How the XamlReader (and particularly markup extensions) deserializes XAML into objects.
  • How the FrameworkElement's resource dictionaries work.
  • How data binding works.

Something like this:

<TextBox Text="{Binding Source={StaticResource MyPerson}, Path=Name}"/>

is engaging (at least) three very different technologies at the same time. Those technologies are all designed to be as flexible as possible, which only makes them more confusing to the beginner. The idea that a binding source can be just about anything: that's hard to grasp. The idea that a markup extension is a special kind of serialization format that supports recursion: simple enough to understand in principle, but a little baffling when you first start working with real-world examples. The idea that a resource dictionary can contain just about anything, and that the resource searching algorithm essentially makes resources inheritable: again, pretty simple in concept, but easy to lose the thread of when you're trying to figure out data binding and XAML at the same time.

It's frustrating, because something that's conceptually simple - "I want to bind this control to a property of an object that I've created" - requires that you understand a great many things before you can actually express it in XAML.

The only solution is to be patient, and to make sure you understand things at the lowest level possible. When you see this:

{StaticResource MyPerson}

you should be able to think, "That's going to invoke the StaticResource

answered 2010-08-29T20:59:15.940

Your Answer