public override void OnApplyTemplate() { // ReSharper disable JoinDeclarationAndInitializer object dpo; // ReSharper restore JoinDeclarationAndInitializer // This invoke is very important, because i have realized that it is not guaranteed that the PasswordBox // Styles and Templates are fully applied at this moment (maybe because they are within a Style ?!) PasswordBoxControl.ApplyTemplate(); // Find the TextBlock in the template, so we are able to apply the watermark text dpo = PasswordBoxControl.Template.FindName("PART_TextBlockHint", PasswordBoxControl); if (dpo is TextBlock) { this.textBlockHint = dpo as TextBlock; this.textBlockHint.Text = this.Watermark; } // Connect the LostMouseCapture event, so we can set the focus to the PasswordBox if neccessary dpo = PasswordBoxControl.Template.FindName("PART_RevealButton", PasswordBoxControl); if (dpo is Button) { (dpo as Button).LostMouseCapture += RevealButton_LostMouseCapture; } // Connect the Click event, so we can handle a mouseclick on the submit button dpo = PasswordBoxControl.Template.FindName("PART_SubmitButton", PasswordBoxControl); if (dpo is Button) { Button submitButton = dpo as Button; submitButton.ToolTip = this.SubmitButtonTooltip; submitButton.Click += SubmitButton_Click; } base.OnApplyTemplate(); }