When working with WPF you all may have come across the term Dependency Property, wondering what this may be and is it ever related to the property provided by CLR? So here I am explaining that with this article. Let's try to understand this amazing feature of WPF. Before doing that, I hope you are more or less familiar with some basic knowledge of the CLR, Object-Oriented Programming and have made at least one simple WPF application.
Table of Contents
- WPF DEPENDENCY PROPERTY
- DEPENDENCY PROPERTY; WHAT IS IT?
- CLR PROPERTY VS. DEPENDENCY PROPERTY
- ADVANTAGES OF DEPENDENCY PROPERTY
- EXAMPLE
- CONCLUSION
Dependency Property; what is it?
WPF has provided some extended services to the CLR property that we can collectively call Dependency Properties. A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the builtin feature of providing notification when the property has changed, data binding and styling.
CLR Property vs. Dependency Property
A CLR property reads directly from the private member of the class. The Get() and Set() methods of the class retrieve and store the values of the property.
Whereas when you set a value of a Dependency Property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.
Advantages of a Dependency Property
Example
To work with a Dependency Property, you must derive the class from a DependencyObject as the entire observer that holds the new Property System is defined within the DependencyObject. We will make a basic car selection application.
XAML
<Window x:Class="WpfApplication1.DependencyPropertyDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="DependencyPropertyDemo" Height="150" Width="250">
<Window.Resources>
<local:DependencyPropertySample x:Key="CarDependency" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="Enter Car:" Grid.Row="0"
VerticalAlignment="Center" />
<TextBox Text="{Binding Path=MyCar,
Source={StaticResource CarDependency }}"
Name="MyTextCar" Height="25" Width="150"
HorizontalAlignment="Right" />
<Button Name="MyButton" Content="Click Me!"
Click="MyButton_Click" Height="25"
Width="150" Grid.Row="1" />
</Grid>
</Window>
Code Behind
public class CarDependencyProperty : DependencyObject
{
//Register Dependency Property
public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(DependencyPropertySample));
public string MyCar
{
get
{
return (string)GetValue(CarDependency);
}
set
{
SetValue(CarDependency, value);
}
}
}
public partial class CarDependencyPropertyDemo : Window
{
public CarDependencyPropertyDemo()
{
InitializeComponent();
}
private void MyButton_Click(object sender, RoutedEventArgs e)
{
CarDependency dpSample =
TryFindResource("CarDependency ") as CarDependency;
MessageBox.Show(dpSample.MyCar);
}
}
Conclusion
Thus we have seen how a Dependency Property can bring wonders to our WPF application. This article is just intended to explain the basics of a Dependency Property. In the future we will see a deeper explanation regarding Dependency Properties. Hope you like it.