Dependency Property Generator 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.
Using the Dependency
Property Generator 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:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
namespaceDeepZoomProject
{
publicpartialclassCustomPage:UserControl
{
publicCustomPage()
{
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.
Integrating your Dependency
Property 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:
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.
Setting Default Values
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:
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.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.