Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Most properties on Silverlight Elements are Dependency Properties. When you create your own elements you should follow the same pattern and create Custom Dependency Properties. Let's review. The Dependency Properties System exists to provide services to your Silverlight Elements. For instance the services like Animation, Data Binding, Control Templates or Styles. Your elements can also partake in these services. If you were to implement your control properties only as .net properties, then sooner or later you will discover that you have created a UI element that cannot be animated or styled and that would be a tragedy.
To participate and create a dependency property, you have to register with the Dependency Property System. That means you have to write what I call ugly registration code. You will see what I mean about this ugliness in a few seconds. Microsoft has a naming convention which you can follow if you like. Microsoft always applies a property suffix to all dependency properties. So the Border Stroke property becomes the BorderStrokeProperty. Now when I say it they sound exactly the same. But if you look at this slide, you can see that they are different. On the bottom of this slide is a short example of how you would register a property.
I call a static member on the dependency class, called register, and I pass in a number of properties. The point string represents the name of the property, the second argument, type of double, represents the data type of the property, the third parameter represents the class that I'm assigning the property to and the fourth property currently set to null represents additional information that I want to pass into the registration system. Let me show you what I mean inside Visual Studio. I am inside a project called CreatingCustomDependencyProperty. I have created my own custom user control down here called the StarShape which looks like this.
I decided that I will like to expose a property in this called points, the number of points on the star. So I went to my code behind and I registered a point's property. Now in my MainPage when I create a star shape I can come down here and set the Points property equal to 5 and the Points property equal to 6. Something is wrong however because one of these stars does not have six points. I'll show you how to fix it. Go here and look at my Dependency registration code again.
This first line represents a field, a member of my type called PointsProperty. I am using Microsoft's naming convention here, it's of data type DependencyProperty. I've marked it as a static member of my type, so it's accessible throughout Silverlight and I've marked it as a readonly. What that means is that I can only instantiate and set this type in the static constructor of my class, which is right here. Inside my static constructor, I am using that same registration code I showed you on the slide a few minutes ago.
To make my code .net friendly, I'm also going to expose a regular .net property called Points. Notice that all I am doing in my getter is calling the dependency property GetValue and then my setter, calling the SetValue. Don't write any other code in here because it might get bypassed by the Silverlight dependency system. Why isn't my points changing? Because I need to register a callback with the dependency system that says, when the point value changes, run some code. I have written a section of code down here which has this method OnPointsChanged and inside this OnPointsChanged virtual method I am looking for the new value.
If the NewValue is or equal to 6, then I show a six point star and I hide my five point star, and if it's any other number other than six, then I show my five point star. I am going to comment out this line of code, and I am going to uncomment these two lines of code here, Ctrl+K, Ctrl+U. Notice what I am doing here. I am setting up my Registration. I am also passing in a new value here in this last argument.
I am doing two things here. I am creating something called a PropertyMetadata and I am assigning the default value, 5 points, and I am also registering my call back. I am saying the PointsChanges where you are going to call back whenever the point number changes. Now when I run the application or I compile it and return back to my main page, I have a 6 point star and now when I double-click on this button and uncomment this line of code, Star1.Points = 6 at runtime it will change my five-point star to a six-point star.
As you have seen, custom dependency properties are tremendously important, especially if you create custom user controls.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96870 Viewers
56 Video lessons · 110563 Viewers
71 Video lessons · 79398 Viewers
131 Video lessons · 38087 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.