wpf Prism PasswordBox数据绑定+提示文本

要点:

1.采用TextBlock+PasswordBox叠加的方式,TextBlock在上层

2.采用DataTrigger数据触发器,绑定PasswordContent(这是附加的依赖属性,原本的PasswordBox是没有的),当PasswordContent的值为空字符串的时候,将TextBlock显示,否则隐藏TextBlock

3.为PasswordBox附加依赖属性——PasswordContent,主要是通过类PassworkBoxHelper实现(详细代码见上篇随笔)

4.该依赖属性的值与PasswordBox的Password属性一样,并且跟随变化(在类PassworkBoxHelper中实现)

5.PasswordContent绑定后台的一个字段,并给该字段赋初值——空字符串

前台代码:

 1 <Style x:Key="LoginPasswordBox" TargetType="TextBox">
 2             <Setter Property="Width" Value="180"/>
 3             <Setter Property="Height" Value="40"/>
 4             <Setter Property="Template">
 5                 <Setter.Value>
 6                     <ControlTemplate TargetType="TextBox">
 7                         <Border BorderBrush="#778899" BorderThickness="1" Background="White" CornerRadius="5" Margin="5">
 8                             <Grid>
 9                                 <Grid.ColumnDefinitions>
10                                     <ColumnDefinition Width="1*"/>
11                                     <ColumnDefinition Width="5*"/>
12                                 </Grid.ColumnDefinitions>
13                                 <Border Grid.Column="0" BorderBrush="#778899" BorderThickness="0,0,1,0" Margin="0,5,5,5"/>
14                                 <Image Source="../Resources/lock1.png" Width="10" Height="10" Margin="0,0,5,0"/>
15                                 <PasswordBox x:Name="passwordbox" Grid.Column="1" Margin="2,0" FontSize="10" BorderBrush="{x:Null}"
16                                          VerticalAlignment="Center" BorderThickness="0"
17                                              base:PasswordBoxHelper.PasswordContent="{Binding PasswordContent}"
18                                              base:PasswordBoxHelper.AttachProperty="True"/>
19                                 <TextBlock x:Name="TipTextBlock" Text=" 请输入密码" Grid.Column="1" Margin="2,0" FontSize="10" Foreground="#DCDCDC"
20                                            Visibility="Collapsed" VerticalAlignment="Center" IsHitTestVisible="False"/>
21                             </Grid>
22                         </Border>
23                         <ControlTemplate.Triggers>
24                             <DataTrigger Binding="{Binding ElementName=passwordbox, Path=(base:PasswordBoxHelper.PasswordContent)}" Value="">
25                                 <Setter TargetName="TipTextBlock" Property="Visibility" Value="Visible"/>
26                             </DataTrigger>
27                         </ControlTemplate.Triggers>
28                     </ControlTemplate>
29                 </Setter.Value>
30             </Setter>
31         </Style>

 

posted @ 2022-06-24 17:27  JustWantToStudy  阅读(659)  评论(0编辑  收藏  举报