Menu

1 / 2

Deep Dive into WPF — Dependency Property (Part 2)

DEV Community: dotnet·yohan·about 5 hours ago
#Y8aPEiMa
#devtodotnet#devto#dev#value#property
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.