[WPF MVVM] PasswordBox Watermark

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
------------------------------------------------PasswordBox Style------------------------------------------------
<Style x:Key="WatermarkPasswordBox" BasedOn="{StaticResource ControlStyle}" TargetType="{x:Type PasswordBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Grid>
<Border x:Name="BorderBase" Background="White" BorderThickness="1.4,1.4,1,1" BorderBrush="Silver">
<Label x:Name="TextPrompt"
Content="********"
Background="{TemplateBinding Background}"
Visibility="Collapsed"
Focusable="False" Foreground="Silver"/>
</Border>
<ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="Black"/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="False"/>
<Condition Property="Tag" Value="f"/><!--PassIsNotEmpty property (default vaalue is "f")-->
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="TextPrompt" Value="Visible"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="DimGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
------------------------------------------------PasswordBox------------------------------------------------
<PasswordBox Grid.Row="3" Style="{StaticResource WatermarkPasswordBox}" Tag="{Binding PassIsNotEmpty}">
<i:Interaction.Behaviors>
<Behaviors:PasswordBehavior Password="{Binding Password, Mode=TwoWay}" />
</i:Interaction.Behaviors>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PasswordChanged">
<i:InvokeCommandAction Command="{Binding PasswordChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
------------------------------------------------ViewModel code------------------------------------------------
[constructor]
PasswordChangedCommand = new Command(OnPasswordChangedCommandExecute);
[/constructor]
public Command PasswordChangedCommand { get; private set; }
private void OnPasswordChangedCommandExecute()
{
PassIsNotEmpty = !string.IsNullOrEmpty(Password) ? "t" : "f";
}
public string PassIsNotEmpty
{
get { return GetValue<string>(PassIsNotEmptyProperty); }
set { SetValue(PassIsNotEmptyProperty, value); }
}
public static readonly PropertyData PassIsNotEmptyProperty = RegisterProperty("PassIsNotEmpty", typeof(string), "f");