Dependency Properties

We've already seen some examples of Dependency Properties in previous chapters, but now let's take a more thorough look. We have a large number of options that we can use when declaring these properties, some more commonly used than others. Let's investigate the standard declaration first, by defining an Hours property of type int in a class named DurationPicker.

public static readonly DependencyProperty HoursProperty =
  DependencyProperty.Register(nameof(Hours), typeof(int), 
  typeof(DurationPicker)); 
 
public int Hours 
{  
  get { return (int)GetValue(HoursProperty); } 
  set { SetValue(HoursProperty, value); }  
} 

As with all Dependency Properties, we start by declaring the property as static and readonly, because we only want a single, ...

Get Mastering Windows Presentation Foundation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.