天天看點

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>
           

繼續閱讀