What Is A Dependency Property In WPF?


In this article, the answer to the question posed in the title is in the name itself: Any property which is dependent on any external source to set their value is known as a dependency property.
Value can be data, which is bound to the property, or style ,which is applied on the property.
Let's go ahead and create a WPF application to understand it practically.
We all are familiar with C# CLR properties, which look something like this:
  1. private int myVar;  
  3. public int MyProperty  
  4. {  
  5.     get { return myVar; }  
  6.     set { myVar = value; }  

Dependency properties are an extension of these CLR properties.
Open your Visual Studio & create a WPF application.
What Is A Dependency Properties In WPF
Go to NuGet package manager and install Prism.wpf because we are going to follow the MVVM design pattern for the same.
What Is A Dependency Properties In WPF
Let's add one textbox, button & text-block. Textblock will display text which was entered in textbox after clicking a button.
So update MainWindow.xaml as follows:
  1. <Window x:Class="DependencyProperty.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.         mc:Ignorable="d"    
  7.         Title="MainWindow" Height="450" Width="800">    
  8.     <Window.Resources>    
  9.         <Style x:Key="TextBlockBottomStyle" BasedOn="{StaticResource {x:Type TextBlock}}"    
  10.                TargetType="TextBlock">    
  11.             <Style.Setters>    
  12.                 <Setter Property="FontSize" Value="40"/>    
  13.                 <Setter Property="FontStyle" Value="Italic"/>    
  14.             </Style.Setters>    
  15.         </Style>    
  16.         <Thickness x:Key="MarginTOP">0 5 0 0</Thickness>    
  17.     </Window.Resources>    
  18.     <Grid>    
  19.         <Grid.RowDefinitions>    
  20.             <RowDefinition Height="Auto"/>    
  21.             <RowDefinition Height="Auto"/>    
  22.             <RowDefinition Height="Auto"/>    
  23.         </Grid.RowDefinitions>    
  24.         <StackPanel Orientation="Horizontal">    
  25.         <Label x:Name="LabelUserName"    
  26.             Content="UserName:"     
  27.             VerticalAlignment="Center"     
  28.             Margin="200 0 0 0"/>    
  29.         <TextBox x:Name="TextBoxName"     
  30.                  Text="{Binding UserName}"     
  31.                  Height="30"     
  32.                  Width="300"/>    
  33.         </StackPanel>    
  34.         <Button x:Name="ButtonSubmit"     
  35.                 Margin="{StaticResource MarginTOP}"     
  36.                 Content="Submit"     
  37.                 Height="20"     
  38.                 Width="100"     
  39.                 HorizontalAlignment="Center"     
  40.                 VerticalAlignment="Center"    
  41.                 Grid.Row="1"/>    
  42.         <TextBlock x:Name="TextBlockUserName"     
  43.                    Text="{Binding UserName}"     
  44.                    Style="{DynamicResource TextBlockBottomStyle}"     
  45.                    HorizontalAlignment="Center"     
  46.                    VerticalAlignment="Top"     
  47.                    Grid.Row="2"/>    
  48.     </Grid>    
  49. </Window>     
Notice that we have created style inside window.resources. And we have boundd this TextBlockBottomStyle to the textblock at the end.
Here, Textblock TextBlockUserName has style property which is bound by dynamic resources.
In the same way, you can see it has Text property which is bound by the CLR property. Here, Text is your CLR property which is encapsulated by Dependency property named TextPropery inside your TextBox's control class.
Let's go ahead and create a MainWindowViewModel.cs class.
  1. using Prism.Mvvm;  
  3. namespace DependencyProperty  
  4. {  
  5.     public class MainWindowViewModel : BindableBase  
  6.     {  
  7.         #region Properties  
  8.         private string _userName;  
  10.         public string UserName  
  11.         {  
  12.             get { return _userName; }  
  13.             set => SetProperty(ref _userName, value);  
  14.         }  
  15.         #endregion  
  16.     }  

You also need to tell MainWindow.xaml class where the dataContext is, so update the MainWindow.XAML.cs, as follows:
  1. using System.Windows;  
  3. namespace DependencyProperty  
  4. {  
  5.     /// <summary>  
  6.     /// Interaction logic for MainWindow.xaml  
  7.     /// </summary>  
  8.     public partial class MainWindow : Window  
  9.     {  
  10.         public MainWindow()  
  11.         {  
  12.             InitializeComponent();  
  13.             this.DataContext = new MainWindowViewModel();  
  14.         }  
  15.     }  

This is what your output would look like:
What Is A Dependency Properties In WPF
I entered Text: User Alex, which is captured by the property Text of TextBoxUserName control.
Textblock TextBlockUserName shows the same text as entered because we have bound the same CLR property: UserName to the Text property of TextBlockUserName.
Also, you can see there is a different style in which it displays text, that's because of the style we applied on TextBlockUserName.
How is this happening? The text being a normal CLR property of a control textbox or textblock, how does it act as a dependency property?
For that, we need to navigate towards the textbox's definition. As you can see there is DependencyProperty Name as TextProperty which encapsulates TextBox's Text property.
What Is A Dependency Properties In WPF
So normal CLR properties are visible as a control's element to the user & encapsulating dependency properties act as a setter to those CLR properties.
WPF uses dependency properties internally and the name of dependency property will always end with a keyword property. It's a standard convention, so they are always static and readonly; e.g. public static readonly DependencyProperty TextWrappingProperty.
  1. Why static?
    This is because it always is available by all the controls & shared among them so it needs to be static. We don't want an object of control to access these properties at their instance.

  2. Why readonly?
    Its value should not change after initialization.
Let's create a dependency property, which will check if text is entered in textbox or not, it will be backed by boolean CLR property.
  1. public static readonly DependencyProperty IsTextEnteredProperty =  
  2.     DependencyProperty.Register(  
  3.     "IsTextEntered"typeof(Boolean),  
  4.     typeof(TextBox)  
  5.     );  
  6. public bool IsTextEntered  
  7. {  
  8.     get { return (bool)GetValue(IsTextEnteredProperty); }  
  9.     set { SetValueIsTextEnteredProperty, value); }  

Now you can use IsTextEntered in TextBox and add your logic to it.
Thank you for reading this article! I hope you understood the concept of dependency properties.
Happy Coding!