WPF 为 PasswordBox 控件添加水印,最低级版

原因也很直接,老板需要,一开始为TextBox发愁,就找了这个控件凑合用,至于版权什么的,内部工具也不卖钱,而且我不懂英文,也就无视了:

 
人家是专业的,所以技术方面我就不研究了,但是,密码框却不能加水印,这倒是挺无聊的。
实在懒得搞控件,就自己写了个样式凑合着用:
提示水印是写死的,毕竟是原生控件,不会搞的就凑合用,看不过去就自己写一个控件吧。
 1     <Style x:Key="PasswordBoxStyle1" TargetType="{x:Type PasswordBox}">
 2         <Setter Property="BorderThickness" Value="1"/>
 3         <Setter Property="Padding" Value="2"/>
 4         <Setter Property="Template">
 5             <Setter.Value>
 6                 <ControlTemplate TargetType="{x:Type PasswordBox}">
 7                    <Border x:Name="Bd" SnapsToDevicePixels="true" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
 8                         <Grid Margin="{TemplateBinding Padding}">
 9                             <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
10                             <TextBlock x:Name="water" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="LightGray" FontSize="{TemplateBinding FontSize}"
11                                        Text="请输入密码" Visibility="Hidden"/>
12                         </Grid>
13                     </Border>
14                     
15                     <ControlTemplate.Triggers>
16                         <Trigger Property="IsEnabled" Value="false">
17                             <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
18                             <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
19                         </Trigger>
20                         <MultiTrigger>
21                             <MultiTrigger.Conditions>
22                                 <Condition Property="IsFocused" Value="false"/>
23                                 <Condition Property="ExtentWidth" Value="0" SourceName="PART_ContentHost"/>
24                             </MultiTrigger.Conditions>
25                             <Setter Property="Visibility" Value="Visible" TargetName="water"/>
26                         </MultiTrigger>
27                     </ControlTemplate.Triggers>
28                 </ControlTemplate>
29             </Setter.Value>
30         </Setter>        
31     </Style>
PasswordBoxStyle1

 

posted @ 2014-09-04 17:06  三台  阅读(786)  评论(0编辑  收藏  举报