I am using a Watermark textbox as in Watermark TextBox in WPF
<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" >
<TextBlock Margin="5,2" Text="This prompt dissappears as you type..." Foreground="{StaticResource brushWatermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Name="txtUserEntry" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" />
</Grid>
How can I apply this for a PasswordBox?
General approach is the same: you write custom control style, and show watermark whenever password box is empty. The only problem here is that PasswordBox.Password property is not a dependency property, and you can't use it in trigger. Also PasswordBox is sealed, so you can't override this notification behaviour. But you can use attached properties here. The following code demonstrates how.
XAML
C#
Please notice PasswordBoxMonitor in XAML code.
you can show/hide the background by yourself instead of using triggers:
XAML:
Code behind:
@blindmeis's suggestion is good. For PasswordBox the class would be as follows.
you can use my approach for a watermark behavior. all you have to do is copy and paste the
TextBoxWatermarkBehavior
and the change theBehavior<TextBox>
toBehavior<PasswordBox>
.you can find a demo project here