Blogger news

Blogger templates

Sunday, February 13, 2011

Dependency Property in Silverlight

Simply we can say dependency property extend the functionality of CLR property. In Silverlight we can say “property that is backed by Silverlight property system is known as dependency property system. When creating dependency property we will register a property to CLR property system.

Most of the properties that are exposed by Silverlight elements (Controls) are dependency property. For example Text property of TextBlock, Content property of button etc. are dependency property. Although dependency features can be get and set like normal CLR properties, they are implemented in a different way that I will explain in this. Dependency property add extra features on the top of CLR property system. Why we need dependency property will discuss later.

Now we can start with the implementation of Dependency Property. Creating dependency property is very simple.

Let’s start discussion with the Silverlight controls dependency property. All Silverlight controls inherit from “FrameWorkElement class. So in the image below displaying all the dependency property that all controls have,

Please click the image to expand.


We can start with custom dependency property. Open Visual studio IDE and select Silverlight application as below,

Please click image to expand

When click ok you will get a window like this,

Again click ok, now the MainPage.XAML will open. Add a Silverlight class as below,

Name the class.

I am going to create a custom TextBox and adding dependency property for that. Code is as below,

using System.Windows;

using System.Windows.Controls;

namespace DependencyPropertySample

{

public class MyTextBox : TextBox

{

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty",

typeof(string),

typeof(MyTextBox), null);

public string MyProperty

{

get

{

return (string)this.GetValue(MyPropertyProperty);

}

set

{

this.SetValue(MyPropertyProperty, value);

}

}

}

}

or click image below to see description,

Now in MainPage.XAML I am including the custom MyTextBox as show image below.

Click image to expand,


I added one button in page and click event of the button control am checking the value of "MyProperty" as below,

using System.Windows;

using System.Windows.Controls;

namespace DependencyPropertySample

{

public partial class MainPage : UserControl

{

public MainPage()

{

InitializeComponent();

}

private void ButtonClick(object sender, RoutedEventArgs e)

{

if (txtMyTextBox.MyProperty == "Hello")

txtMyTextBox.Text = "My Property value is HELLO!!!";

}

}

}

or click image below,

Dependency Property objects cannot instantiated directly as "DependencyObject" class has no public constructor. Instance can be created only using static DependencyProperty.Register() method. All DependencyProperty members are read only.

Property Meta Data

The property meta data parameter provides additional information. PropertyMetaData to store optional pieces of information such as default value of property and call back that will be triggered when the property is changed. If we are not using these then we can pass null as i did in sample.

This is all about the simple basics of Dependency Property in Silverlight.

No comments:

Post a Comment