Picking up from the previous article , we introduced the principles behind dependency properties and implemented a simple DependencyProperty (DP). This article focuses on how to use DPs and what to watch out for.
Review
Dependency properties are used by DependencyObject. So what does a typical usage scenario look like?
Using DependencyProperty
A simple usage looks like this:
1 : public class SimpleDO : DependencyObject 2 : { 3 : public static readonly DependencyProperty IsActiveProperty = 4 : DependencyProperty . Register ( "IsActive" , typeof ( bool ), typeof ( SimpleDO ), 5 : new PropertyMetadata (( bool ) false )); 6 : 7 : public bool IsActive 8 : { 9 : get { return ( bool ) GetValue ( IsActiveProperty ); } 10 : set { SetValue ( IsActiveProperty , value ); } 11 : } 12 : }
SimpleDO sDo = new SimpleDO (); sDo . IsActive = true ;
Enter fullscreen mode
Exit fullscreen mode
Here we register the DP with DependencyProperty.Register.…