14.10. Attached and Dependency Properties

When you set a property of an object in ASP.NET such as Width on a TextBox, you would usually write something like this:

<asp:TextBox Width="300" Runat="server" />

Behind the scenes, the Width value is retained in a private field. If you were to retrieve the Width property, it would be the same as when it was first set. However, when working with dependency properties, this is not necessarily true.

Consider how a browser works out where to display an item on the screen. It is not as straightforward as it first sounds. There are actually a number of things that need to be taken into account when calculating where an item will be positioned on a page:

  • Other elements on the page

  • Runtime settings (e.g., screen and browser settings)

  • Styles

  • Templates

That's quite a lot to consider. You could say the positioning is dependent on lots of other factors. Dependency properties are properties that are dependent on other property values. It's as simple as that, and it saves you lots of work, and not just for tasks such as positioning elements. Dependency properties are also used for tasks such as data binding where Silverlight needs to know to redraw an item if the underlying data source were to change.

A predefined order exists that determines how dependency properties will be evaluated, which is intuitive for the most part. For more information on how dependency properties are evaluated, please refer to http://msdn.microsoft.com/en-us/library/ms743230.aspx.

Attached properties are a type of dependency property, but attached properties do not necessarily exist in the object they are set on. They are easy to spot because are written in this format:

AttachedPropertyProvider.PropertyName

With the theoretical stuff out the way, let's look at the controls that allow you to position elements on a page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.231