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, immutable instance of it. This also enables us to access it without an instance of our class.
Note that this readonly
declaration does not mean that we cannot set the value of our property. Unlike normal CLR properties...