A
DependencyProperty
is set to enable declarative code to alter the properties of an object which reduces the data requirements by providing a more powerful notification system regarding the change of data in a very specific way. In .NET, there are two types of properties: the normal property, and a Dependency Property which adds functionality over the normal property.
Now, let us discuss on how to implement such a Dependency Property to give a powerful notification on a data change.
First of all, implement the
UserControl
class from the
INotifyPropertyChanged
interface:
public
partial
class
MyUserControl : UserControl, INotifyPropertyChanged
public
event
PropertyChangedEventHandler PropertyChanged;
protected
void
OnPropertyChanged(
string
propertyName)
if
(PropertyChanged !=
null
)
PropertyChanged(
this
,
new
PropertyChangedEventArgs(propertyName));
Create your own normal property. Let's say the name of the property is “
Caption
”.
public
string
Caption
get
{
return
GetValue(CaptionProperty).ToString(); }
set
{ SetValue(CaptionProperty,
value
); }
Now, register the
DependencyProperty
to the CLR by calling the
Register
method and by passing the property field that you used to store the data in the earlier step:
public
static
readonly
DependencyProperty CaptionProperty =
DependencyProperty.Register(
"
Caption"
,
typeof
(
string
),
typeof
(MyUserControl),
new
PropertyMetadata(
string
.Empty, OnCaptionPropertyChanged));
The name of the identifier field of the
DependencyProperty
will be same as you used in the property after appending “Property” at the end. In this example, our property name is “
Caption
”, hence our identifier field name is “
CaptionProperty
”. Add the
PropertyMetaData
with the default value and the callback event handler within the
Register
method as mentioned in the above code. Mark the identifier as
static
and
readonly
so that this will be unique to the CLR.
Now, implement the
OnCaptionPropertyChanged
event handler:
private
static
void
OnCaptionPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
MyUserControl myUserControl = dependencyObject
as
MyUserControl;
myUserControl.OnPropertyChanged(
"
Caption"
);
myUserControl.OnCaptionPropertyChanged(e);
private
void
OnCaptionPropertyChanged(DependencyPropertyChangedEventArgs e)
txbCaption.Text = Caption;
The implementation of the Dependency Property is complete. You can either call it from XAML:
<
local:MyUserControl
Caption
="
My First Dependency Property Example"
/
>
or from the code-behind:
MyUserControl myUserControl =
new
MyUserControl();
myUserControl.SetValue(MyUserControl.CaptionProperty,
"
My First Dependency Property Example"
);
License
Kunal Chowdhury
is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (
www.kunal-chowdhury.com
) and CodeProject.
Books authored:
Connect with Kunal on:
What is txbCaption OnCaptionPropertyChanged event. It seems to be a textblock but its a MyUserControl class so where this XAML control came???? not clear....
Sign In
·
View Thread
My vote of 3
Nice layout of syntax, but it did not really help with concepts.
Changed from 3 to 4 because it helped me out of a bind.
modified 18-Aug-13 11:31am.
Sign In
·
View Thread
Very helpful post. The "move" in the post for me was the switch from the static "OnCaptionPropertyChanged" method to the instance "OnCaptionPropertyChanged" method.
I haven't seen that elsewhere.
thanks.
Sign In
·
View Thread
Can you tell me why the Identifier should be defined readonly?
And if its readonly how is the Set {} is used.
-Hemant
Sign In
·
View Thread
Hi phobik,
Can you explain me what was badly explained there? Please provide your suggestion to improve the article.
Sign In
·
View Thread
Hi Uday,
Sorry for the late reply but the method
OnCaptionPropertyChanged
should not be static. One is already static but the another will never be static.
private
static
void
OnCaptionPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
private
void
OnCaptionPropertyChanged(DependencyPropertyChangedEventArgs e)
The first one is static but the second one we are calling from the instance of the object. So, never be static. If you have any concerns, please let me know.
Don't forget
to Click on
[Vote]
and
[Good Answer]
on the posts that helped you.
Regards -
Kunal Chowdhury | Software Developer | Chennai | India |
My Blog
|
My Tweets
|
Silverlight Tutorial
Sign In
·
View Thread
Web04
2.8:2023-05-13:1