Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Properties, Dependency Properties, and WPF


Instantiation and Initialization

Let's look at some C# code first. Here is some code to create and initialize a ScrollBar control, perhaps in the constructor of a Window class where the ScrollBar appears:


ScrollBar scr = new ScrollBar();
scr.Orientation =      Orientation.Vertical;
scr.Minimum = 10;
scr.Maximum = 100;

Orientation, Minimum, and Maximum look like fields of the ScrollBar class, but they are actually properties. The Orientation property is set to a member of the Orientation enumeration; the other properties are of type double. Properties were introduced in .NET 1.0 as a nice clean syntax to replace pairs of Get and Set methods; for example, GetMinimum and SetMinimum.

Very often, a .NET property provides a public interface to a private field. A property such as Minimum would be associated with a private field initialized to a default value:


private double min = 0;

The definition of the Minimum property starts out looking like a method, but without any parentheses or argument list. Within the property definition are two accessors, labeled get and set with code that might otherwise be found in the corresponding GetMinimum and SetMinimum methods:


public double Minimum
{
    get
    {
        return min; 
    }
    set
    {
        min = value;
        ...
    }
}

The get accessor must terminate with a return instruction with a value of the proper type. Within the set accessor, the word value indicates the value the property is being set to. Notice that ominous ellipsis. Traditionally, what happens next depends on how the ScrollBar class is structured. The object might need to redraw itself based on the new value of Minimum. Perhaps the Value property of ScrollBar is no longer within the range between Minimum and Maximum. Value might then need to be reset, which might trigger an event.

Properties are sometimes called "smart fields" because code is evoked when a property is set or retrieved. The object has the opportunity to react in some way, and that's just not the case with fields. An object doesn't know a field has been set until it checks the value.

My illustration of the Minimum property is actually not how properties like these are defined in the Windows Presentation Foundation. Some properties are, but many are not. Many WPF properties are actually a somewhat different kind of animal called "dependency properties," and the reasons for the extra overhead in the WPF is one of the themes of this article.

Originally, .NET properties seemed to do little more than provide an elegant syntax to replace Set and Get methods. A bonus was revealed when it was time to represent classes in XML-based files, including XAML. The property names simply became XML attributes. Here's the equivalent XAML of the ScrollBar creation and initialization logic:

     
<ScrollBar
  Orientation="Horizontal"
  Minimum="0"
  Maximum="150"
/>

The problem with this particular ScrollBar is that it doesn't have any effect on anything else in the program. In the general case, you'll want an event handler that's triggered whenever the Value property of the ScrollBar changes. You indicate the method you want to use as the handler for the ValueChanged event in C# code like this:


scr.ValueChanged += 
  ScrollBarValueChanged;

In XAML, you can indicate the event handler like so:


<ScrollBar 
  Orientation="Horizontal"    
  Minimum="0" Maximum="150"
  ValueChanged="ScrollBarValueChanged"
/>

However, the handler itself needs to be written in code.

There's another way the ScrollBar could be used, and this alternative approach doesn't require an event handler. It's a data binding. Basically, you establish a link between the Value property of the ScrollBar and a property of some other object. You can establish this binding in code, but it's actually easier to do it in XAML.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.