by
kirupa | 25 February 2009
In C#, creating
properties in code is very easy. A property called
AuthorName would look
like the following:
- public
string
AuthorName
- {
- get;
- set;
- }
The above code, known
as a CLR property, is good enough for you to use in
most cases that involve setting and accessing a
value. A slight wrinkle in this perfect fabric
occurs when you are using Silverlight and want to do
more.
While you can still
use properties such as what I've shown above,
Silverlight introduces more ways to set and access a
value - ways that a CLR property simply cannot
handle. Some of these ways involve animations, data
binding, styling/templating, and more. They go beyond simple setting
and getting via code.
To deal with this, you
have a new type of a property known as a dependency
property. In this article, I will show you how to
create a dependency property in Silverlight.
The best
way to get introduced to dependency properties is to
look in detail at what one looks like. The above
AuthorName property
defined as a dependency property will look as
follows:
- public
string
AuthorName
- {
- get
{
return
(string)GetValue(AuthorNameProperty);
}
- set
{
SetValue(AuthorNameProperty,
value);
}
- }
-
- public
static
readonly
DependencyProperty
AuthorNameProperty
=
DependencyProperty.Register("AuthorName",
typeof(string),
typeof(AuthorTextField),
new
PropertyMetadata(""));
Whoa! What was a few
lines earlier now has turned into more than a few
lines of mostly incomprehensible code. Let's try to
make sense of all this.
The basic structure of
a dependency property is made up of two things. First,
you have your CLR wrapper:
- public
string
AuthorName
- {
- get
{
return
(string)GetValue(AuthorNameProperty);
}
- set
{
SetValue(AuthorNameProperty,
value);
}
- }
This wrapper provides
you with easy access to the AuthorName property just
like you would expect with any old CLR property that
you can declare. Internally, it is a bit different
in that the get and
set methods call
GetValue and
SetValue
respectively on a property called
AuthorNameProperty.
The CLR wrapper is the first item that makes up your
dependency property. The second item is what is
known as a dependency property identifier, and in
our code, that is represented by
AuthorNameProperty:
- public
static
readonly
DependencyProperty
AuthorNameProperty
=
DependencyProperty.Register("AuthorName",
typeof(string),
typeof(AuthorTextField),
new
PropertyMetadata(""));
This line is what
reconciles your above CLR wrapper with the property
system that lives beneath Silverlight. This line can
be divided into two parts as well. The first part is
where you simply declare the identifier:
- public
static
readonly
DependencyProperty
AuthorNameProperty
=
DependencyProperty.Register("AuthorName",
typeof(string),
typeof(AuthorTextField),
new
PropertyMetadata(""));
Almost always, your
type will be DependencyProperty,
and its modifiers will be public,
static, and readonly. The
second part is where you initialize your dependency
property identifier by registering it:
-
public
static
readonly
DependencyProperty
AuthorNameProperty =
DependencyProperty.Register("AuthorName",
typeof(string),
typeof(AuthorTextField),
new
PropertyMetadata(""));
Registering your
property is interesting because it really doesn't
make a lot of sense because the behind the scenes
details are hidden from view. The main thing to
remember is that, deep under the hoods, Silverlight
has an advanced property system that keeps track of
the various dependency properties used in an
application.
Registering is where the various pieces
of data that make up your dependency property get
stored in that behind-the-alley warehouse that only
Silverlight can access. In the next page, let's look
at this registration process in greater detail.
Onwards to the
next page!
|