天天看点

WPF--->PasswordBox使用方法,全网最清晰简明,简单的自定义PasswordBox样式PasswordBox的使用Style自定义样式

文章目录

  • PasswordBox的使用
    • xaml代码
    • c#后台
  • Style自定义样式

PasswordBox的使用

PasswordBox无法直接对Password与SecurePassword进行Binding,

如下使用Password与SecurePassword进行密码的读取

xaml代码

<Grid>
        <StackPanel VerticalAlignment="Center">
			<PasswordBox x:Name="PasswordBox1" Height="30" Width="105" FontSize="20"/>
		<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
			<Button x:Name="Button1" Height="30" Width="120" Click="Button1_Click" Content="UsePassword" Margin="20"/>
			<Button x:Name="Button2" Height="30" Width="120" Click="Button2_Click" Content="UseSecurePassword" Margin="20"/>
		</StackPanel>
		</StackPanel>
	</Grid>
           

c#后台

private void Button1_Click(object sender, RoutedEventArgs e)
        {
            string password = this.PasswordBox1.Password;
            // 显示
            _ = MessageBox.Show(password);
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            // 使用一个IntPtr类型值来存储加密字符串的起始点 
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.PasswordBox1.SecurePassword);
            // 使用.NET内部算法把IntPtr指向处的字符集合转换成字符串 
            string password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            // 显示
            _ = MessageBox.Show(password);
        }
           

Style自定义样式

对于功能需求没有改变的话就不需要C#代码了,如果有需求(密码验证)再加也是可以的

<Style TargetType="PasswordBox">
	<Setter Property="Background" Value="#ffffff"/>
	<Setter Property="BorderBrush" Value="#b4b4b4"/>
    <Setter Property="BorderThickness" Value="1"/>
	<Setter Property="PasswordChar" Value="✱"/>
    <Setter Property="FontSize" Value="12"/>
	<Setter Property="Foreground" Value="#5f5f5f"/>
    <Setter Property="Padding" Value="2 0 0 0"/>
    <Setter Property="VerticalContentAlignment" Value="Bottom"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
	<Setter Property="Height" Value="25"/>
	<Setter Property="Width" Value="150"/>
	<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="MinHeight" Value="30"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
       <Setter.Value>
            <ControlTemplate TargetType="PasswordBox">
					<Border x:Name="PART_Border" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
						<ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center"
                                      HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    </Border>
                    <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="PART_Border" Value="0.4"/>   
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
							<Setter Property="BorderBrush" TargetName="PART_Border" Value="#2561a9"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="true">
							<Setter Property="BorderBrush" Value="#2561a9" TargetName="PART_Border"/>
                    </Trigger>
                    </ControlTemplate.Triggers>
            </ControlTemplate>
       </Setter.Value>
    </Setter>
</Style>
           

继续阅读