What is a DependencyProperty in SIlverlight and How to implement it?

What is a DependencyProperty and How to implement it?  Please explain in detail with an example.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aaron JabamaniTechnical ArchitectCommented:
you can have custom Property to any existing control for example Textbox. You can access this property in XAML or code behind. Actually all default properties of textbox are dependency properties

for ex. lets say i want to have custom property called Maxlength for my TexBox. We should have below codes.

public static readonly DependencyProperty MaxLengthProperty =
            //the name u want to appear in inetllisense in xaml
             //under which class you have this dependcy property, here i have a custom textbox control class
            //default value for this property
            new PropertyMetadata(0));

        public int MaxLength
            get { return (int)GetValue(MaxLengthProperty); }

                //have your custom validations here which will be captured in runtime.
                if (!int.TryParse(value.ToString(), out i))
                    throw new ArgumentException("Invalid MaxLength Argument");
                if (i < 1)
                    throw new Exception("Max Legth value should be greater than Zero");
                SetValue(MaxLengthProperty, value);

now u can access MaxLength to get value from XAML or set value in code behind. The same can be used in Binding as well.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.