How do You Create a Read-Only Dependency Property?
It's easy, actually (via RegisterReadOnly):
public class OwnerClass : DependencyObject // or DependencyObject inheritor{ private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey = DependencyProperty.RegisterReadOnly( nameof(ReadOnlyProp), typeof(int), typeof(OwnerClass), new FrameworkPropertyMetadata(default(int), FrameworkPropertyMetadataOptions.None)); public static readonly DependencyProperty ReadOnlyPropProperty = ReadOnlyPropPropertyKey.DependencyProperty; public int ReadOnlyProp { get { return (int)GetValue(ReadOnlyPropProperty); } protected set { SetValue(ReadOnlyPropPropertyKey, value); } } //your other code here ...}
You use the key only when you set the value in private/protected/internal code. Due to the protected ReadOnlyProp
setter, this is transparent to you.