by
kirupa | 29 March 2009
This site has a
fair number of articles on
dependency properties, but at the end of the day,
they are still really hard to write. Remembering the
syntax and making sure the C# equivalent of the i's
and t's are dotted and crossed is very hard - even
for experienced users.
To make this tedious task easier, I've written a
small app that will create Dependency Properties for
you as long as you give it the type, owning class,
and property name. The app can be found below:
If you are a reader of my blog, the above
application
should be old news to you. What will be new is
the information on how to actually use the app and
integrate its output into your own project.
This little app asks
for three pieces of information. First, you will be asked to enter your
Property Name.
This is the name you want to give your dependency
property so that you can use it later on. To look at
it another way, this is the value that you will see
in the Properties Inspector of either Expression
Blend or Visual Studio.
The next piece of information you will be asked
is the Property Type.
Your property type represents what form the data you
store in the dependency property will be taking. Example values include
string, double, int,
MyCustomType, List, etc.
Finally, you will be asked the
Owning Class Name.
Your dependency property will be living inside
something. This something is a class that you paste
your dependency property code into. For example,
let's look the code for a simple user control that I
just created in Expression Blend:
using
System;
using
System.Collections.Generic;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
namespace
DeepZoomProject
{
public
partial
class
CustomPage
:
UserControl
{
public
CustomPage()
{
InitializeComponent();
}
}
}
This user control's name is CustomPage, and my dependency property will live inside this
usercontrol. This name is also the class name, so therefore, my Owning Class Name in this case will be
CustomPage.
To close this example out, let's say I want to
create a dependency property. My property will be
called CustomName, its type will be
string, and its owning class is
CustomPage. I hit the Generate Code
button, and out pops the following:
public
string
CustomName
{
get
{
return
(string)GetValue(CustomNameProperty);
}
set
{
SetValue(CustomNameProperty,
value);
}
}
public
static
readonly
DependencyProperty
CustomNameProperty
=
DependencyProperty.Register("CustomName",
typeof(string),
typeof(CustomPage),
new
PropertyMetadata("",CustomNameChanged));
private
static
void
CustomNameChanged(DependencyObject
d,
DependencyPropertyChangedEventArgs
e)
{
// Code for
dealing with your property changes
}
Pasting the above into my CustomPage class, I get
the following:
using
System;
using
System.Collections.Generic;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
namespace
DeepZoomProject
{
public
partial
class
CustomPage
:
UserControl
{
public
string
CustomName
{
get
{
return
(string)GetValue(CustomNameProperty);
}
set
{
SetValue(CustomNameProperty,
value);
}
}
public
static
readonly
DependencyProperty
CustomNameProperty
=
DependencyProperty.Register("CustomName",
typeof(string),
typeof(CustomPage),
new
PropertyMetadata("",CustomNameChanged));
private
static
void
CustomNameChanged(DependencyObject
d,
DependencyPropertyChangedEventArgs
e)
{
// Code
for dealing with your property
changes
}
public
CustomPage()
{
InitializeComponent();
}
}
}
Notice that I pasted my dependency property code
inside the area defined by my CustomPage class. The
most common mistake is that you paste it outside
your owning class's boundaries. Don't do that unless
you want to see a build error or your dependency
property not working.
That is all there is to filling in the three
values, getting the code for your dependency
property, and then simply pasting the code into the
class you want the property to be exposed on.
The final detail is the default value that your
dependency property may require. Currently, outside
of string,
double, and
int types, I do
not specify a default value for your dependency
property. The reason is that each type will have its
own unique way of specifying a default value for
itself.
The default value is specified in the line
defining your dependency property identifier:
public
static
readonly
DependencyProperty
CustomNameProperty
=
DependencyProperty.Register("CustomName",
typeof(string),
typeof(CustomPage),
new
PropertyMetadata("",CustomNameChanged));
More specifically, look in your PropertyMetadata
constructor: new PropertyMetadata("",CustomNameChanged));
If there are two arguments, the first argument
specifies the default value. For property types
other than string, int, and double, you will not see
two arguments. The only argument you see will be for
the property changed callback, so insert your
default value as the first argument to your
PropertyMetadata constructor.
Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!
When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.
Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.
This is a companion discussion topic for the original entry at https://www.kirupa.com/blend_silverlight/dependency_property_generator.htm