WPFPasswordBox点击眼睛查看明⽂样式
其他资源:
效果
其他资源查看上⼀篇:
Style:
<Style x:Key="EyePasswordBox" TargetType="{x:Type PasswordBox}">
<Setter Property="SelectionBrush" Value="{StaticResource PasswordBox.Static.SelectionBrush}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="FontSize" Value="22"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="PasswordChar" Value="●"/>
<Setter Property="Background" Value="{StaticResource PasswordBox.Static.Background}"/>
<Setter Property="Foreground" Value="{StaticResource PasswordBox.Static.Foreground}"/>
<Setter Property="BorderBrush" Value="{StaticResource PasswordBox.Static.BorderBrush}"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="CaretBrush" Value="{StaticResource PasswordBox.Static.Foreground}"/>
<Setter Property="controls:PasswordBoxHelper.Attach" Value="True"/>
<Setter Property="controls:PasswordBoxHelper.Password" Value="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Border x:Name="PART_Root" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
CornerRadius="{TemplateBinding controls:ControlAttachProperty.CornerRadius}"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<Grid x:Name="PART_InnerGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Path=(controls:ControlAttachProperty.CornerRadius).TopLeft, RelativeSource={RelativeSource TemplatedP                            <ColumnDefinition  Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition  Width="Auto"/>
<ColumnDefinition Width="{Binding Path=(controls:ControlAttachProperty.CornerRadius).TopLeft, RelativeSource={RelativeSource TemplatedP                        </Grid.ColumnDefinitions>
<TextBlock x:Name="PART_ContentIdentity" Grid.Column="1"
FontSize="20"
FontFamily="/ImageButtonTest01;component/Resources/#iconfont"
Text=" "
HorizontalAlignment="Center" VerticalAlignment="Center"/>
<!--内容区域-->fontweight取值
<ScrollViewer x:Name="PART_ContentHost" Grid.Column="2" Margin="2"
BorderThickness="0" IsTabStop="False"
VerticalAlignment="Stretch" Background="{x:Null}"
VerticalContentAlignment="Center"/>
<!--显⽰密码明⽂-->
<TextBox x:Name="PART_PasswordShower" Grid.Column="2"
IsTabStop="True" Focusable="False"
Margin="2" Padding="0"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
BorderBrush="Transparent" BorderThickness="0"
Visibility="Collapsed" IsHitTestVisible="False"
Width="{Binding CaretIndex, RelativeSource={RelativeSource TemplatedParent}}"
Text="{Binding Path=(controls:PasswordBoxHelper.Password), RelativeSource={RelativeSource TemplatedParent}}"
FontSize="{TemplateBinding FontSize}"
CaretBrush="Transparent"/>
<TextBlock x:Name="PART_PlaceHolder" Grid.Column="2" Foreground="Black"
IsHitTestVisible="False" Margin="3,0,3,0"
HorizontalAlignment="Left" VerticalAlignment="Center"
SnapsToDevicePixels="True"
Visibility="Collapsed" Opacity="0.6"
TextAlignment="Center"
Text="{Binding Path=(controls:ControlAttachProperty.PlaceHolder), RelativeSource={RelativeSource TemplatedParent}}"                                FontSize="{Binding FontSize, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource PHFSC}}"/>
<!--附加内容区域-->
<ToggleButton x:Name="PART_EyeToggle" Grid.Column="3"
IsTabStop="True" Focusable="False"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{Binding Background,RelativeSource={RelativeSource TemplatedParent}}"
Foreground="Black" FontWeight="Medium"
IsEnabled="True"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource PasswordBoxToggleButtonStyle}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="controls:PasswordBoxHelper.Password" Value="">
<Setter TargetName="PART_PlaceHolder" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_EyeToggle" Property="IsEnabled" Value="False"/>
</Trigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=PART_EyeToggle}" Value="True">
<Setter TargetName="PART_PasswordShower" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_PasswordShower" Property="IsHitTestVisible" Value="True"/>
<Setter TargetName="PART_PasswordShower" Property="CaretBrush" Value="Black"/>
<Setter TargetName="PART_PasswordShower" Property="Focusable" Value="True"/>
<Setter  Property="BorderBrush" Value="{StaticResource PasswordBox.Focused.BorderBrush}"/>
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter  Property="BorderBrush" Value="{StaticResource PasswordBox.MouseOver.BorderBrush}"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter  Property="BorderBrush" Value="{StaticResource PasswordBox.Focused.BorderBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="PART_Root" Property="Opacity" Value="0.4"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ToggleButton:
<Style x:Key="PasswordBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="Transparent" Margin="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="txt" Grid.Column="1"
HorizontalAlignment="Center" VerticalAlignment="Center"
FontFamily="/ImageButtonTest01;component/Resources/#iconfont"
Text=" " FontSize="20"
Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="txt" Property="Text" Value=" "/>
<Setter TargetName="txt" Property="Foreground" Value="{StaticResource ComboToggleButton.Checked.Foreground}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="txt" Property="Foreground" Value="{StaticResource ComboToggleButton.MouseMove.Foreground}"/>                    </Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
使⽤:
<PasswordBox x:Name="usrPassword3" Width="150" Height="40"
Style="{StaticResource EyePasswordBox}"
Margin="3"
control:ControlAttachProperty.PlaceHolder="请输⼊密码"
control:ControlAttachProperty.CornerRadius="10"
IsEnabled="True"/>
<PasswordBox x:Name="usrPassword4" Width="150" Height="40"
Style="{StaticResource EyePasswordBox}"
Margin="3"
control:ControlAttachProperty.PlaceHolder="请输⼊密码"
IsEnabled="True"/>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。