When use registerReadonly

使用:

internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
  "AquariumSize",
  typeof(double),
  typeof(Aquarium),
  new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
  AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
  get { return (double)GetValue(AquariumSizeProperty); }
}


通常将DependencyPropertyKey作为internal, 然后expose一个public dependency properpty ,然后写上get ,使其能在xaml使用。

一个典型应用是在style中trigger这个property, 而不用设置,这个property.

设置这个property则应该使用一个安全的内部方法:
/// <summary>
        /// Provides a secure method for setting the AquariumSize property. 
        /// This dependency property indicates whether the Dialog is open.
        /// </summary>
        /// <param name="value">The new value for the property.</param>
        protected void SetAquariumSize (bool value)
        {
            SetValue(AquariumSizeKey , value);
        }

比如:IsDialogOpen Read-Only Dependency Property

原文地址:https://www.cnblogs.com/liangouyang/p/1335787.html