public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { if (!(sender is PasswordBox passwordBox)) { throw new NotSupportedException($"{nameof(MonitorPasswordProperty)} may only be attached to a PasswordBox"); } // Remove any previous events passwordBox.PasswordChanged -= PasswordBox_PasswordChanged; // If the caller set MonitorPassword to true... if ((bool)e.NewValue) { // Set default value HasTextProperty.SetValue(passwordBox); // Start listening out for password changes passwordBox.PasswordChanged += PasswordBox_PasswordChanged; } }