wpf带水印的密码框样式
时间: 2023-10-23 17:37:26 浏览: 32
以下是一个简单的WPF带水印的密码框样式,你可以根据自己的需求进行修改:
```xml
<Style TargetType="PasswordBox" x:Key="WatermarkPasswordBoxStyle">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="PasswordBox">
<Grid>
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}">
<PasswordBox x:Name="PART_PasswordBox"
Foreground="{TemplateBinding Foreground}"
Background="Transparent"
BorderThickness="0" />
</Border>
<TextBlock x:Name="PART_Watermark"
Text="Enter password"
Foreground="Gray"
Visibility="Collapsed"/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="False"/>
<Condition Property="PasswordBox.Password" Value=""/>
</MultiTrigger.Conditions>
<Setter TargetName="PART_Watermark" Property="Visibility" Value="Visible"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="PART_Watermark" Property="Foreground" Value="LightGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
使用方法:
```xml
<PasswordBox Style="{StaticResource WatermarkPasswordBoxStyle}"/>
```