Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner - confused about binding and resources in WPF

Tags:

binding

wpf

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 to the binding subsystem? And when exactly is that object created? 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.

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:

<local:Person x:Key="MyPerson" Name="Title"/>  

(where Person is a class with a property called Name) and then in the window I have:

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

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?

<TextBlock Text="{StaticResource MyPerson, Path=Name}"/>  

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

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?)

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)); } 

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)

c) I tried both:

Text="{Binding Source={DynamicResource HeaderText}, Path=Header}" 

Which gave me an exception, and

Text="{DynamicResource HeaderText}" 

But I couldn't understand where to put the path to the Header property.

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.

like image 727
OrWhen Avatar asked Aug 28 '10 16:08

OrWhen


People also ask

How many types of binding are there in WPF?

WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise. OneWay: The target property will listen to the source property being changed and will update itself.

What's the difference between StaticResource and DynamicResource in WPF?

StaticResource are retrieved only once by the referencing element and used for entire life of the resource. On the other hand, DynamicResource are acquired every time the referenced object is used.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What default resources are supported in WPF?

WPF supports different types of resources. These resources are primarily two types of resources: XAML resources and resource data files. Examples of XAML resources include brushes and styles. Resource data files are non-executable data files that an application needs.


1 Answers

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 markup extension handler, which retrieves an object from a resource dictionary using the key MyPerson when the XAML is deserialized.

It's extremely challenging at first. I've been developing software professionally for 35 years, and I've found WPF to be the most challenging technology platform that I've ever learned by a considerable margin. But all of this stuff is hard to learn because it's incredibly functional and flexible. And the payoff of learning it is huge.

To address a couple of issues that karmicpuppet didn't:

From my experience in MFC [resources] were icons, strings, etc.

That hasn't changed. You can still create resource files in WPF and load them into objects at runtime. There are lots of different ways of doing this - you can create resources in the resource editor and load them via the Properties.Resources object, you can add image files (for instance) to the project, have them compiled as resources, and load them using their URI, and there are plenty of other ways that I don't know about.

The resources available to FrameworkElements via their resource dictionaries are a different thing. Well, sort of. Here's an example:

<Window.Resources>    <Image x:Key="MyImage" Source="images/myimage.png"/> </Window.Resources> 

This creates an Image object and adds it to the Window's resource dictionary with a key of MyImage You can then reference that object via the StaticResource markup extension in XAML, or the FindResource method in code.

Setting the Source attribute on the Image element in XAML also makes the XamlReader use the ResourceManager to read the image data from the project's compiled resources at runtime when it creates the Image object.

In practice, this is nowhere near as confusing as it is when you're first learning WPF. I never get resources that ResourceManager loads and resources stored in resource dictionaries mixed up.

And when exactly is that object created?

Any object defined by a XAML element is created when the XamlReader reads the element. So this:

<Window.Resources>    <local:Person x:Key="MyPerson"/> </Window.Resources> 

instantiates a new Person object and adds it to the Window's resource dictionary with a key of MyPerson. It's exactly equivalent to doing this in the Window's code-behind:

AddResource("MyPerson", new Person()); 

So why don't you just do it in code-behind? Two reasons:

First, it's consistent. If you define all your resources in XAML, you only need to look in XAML files to find what your resources are. If you define them in both XAML and code-behind, you have to look in two places.

Second, the IDE knows about resources that you define in XAML. If you type

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

in your XAML, the IDE will let you know if you haven't defined, somewhere in the hierarchy of resource dictionaries, a resource whose key is MyPerson. But it doesn't know about resources that you've added in code, and so even though the resource may actually be findable at runtime, the IDE will report it as a problem.

like image 101
Robert Rossney Avatar answered Sep 27 '22 18:09

Robert Rossney