WPF中的密码框

XAML文件 :

<PasswordBox Name="PasswordBox" Margin="5" Padding="5" PreviewKeyDown="Txtpassword_PreviewKeyUp"/>
后台代码:

IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.PasswordBox.SecurePassword);
            string password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);