1. 程式人生 > >WPF 使用TextBox做密碼輸入框

WPF 使用TextBox做密碼輸入框

密碼輸入框需要輸入的密碼不能顯示明文,用其他的特殊字元代替顯示。
顯示效果如下:
這裡寫圖片描述

Xaml部分程式碼如下:

<Window x:Class="TextBoxPwd.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextBoxPwd" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <StackPanel> <TextBox FontFamily="Courier New" x:Name="pwd" FontSize="20" Foreground="Transparent" Text="sdfsddfsfs"/> <TextBox FontFamily
="Courier New" FontSize="20" Text="sdfsddfsfs"/> </StackPanel> </Window>

後臺程式碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using
System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace TextBoxPwd { /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); pwd.TextDecorations = new TextDecorationCollection(new TextDecoration[] { new TextDecoration() { Location= TextDecorationLocation.Strikethrough, Pen= new Pen(Brushes.Black, 10f) { DashCap = PenLineCap.Round, StartLineCap= PenLineCap.Round, EndLineCap= PenLineCap.Round, DashStyle= new DashStyle(new double[] {0.0,1.2 }, 0.6f) } } }); } } }