Custom password input box in windowsphone7

Source: Internet
Author: User

The passwordbox provided in the SDK is easy to use, but it cannot display the password.

You can combine phonetextbox and passwordbox to customize a user control, which is used as the password input box. You can choose to hide or display the password.

XAML code:

<Usercontrol X: class = "customcontrols. passwordtextbox "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: TK =" CLR-namespace: Microsoft. phone. controls; Assembly = Microsoft. phone. controls. toolkit "MC: ignorable =" D "fontfamily =" {staticresource phonefontfamilynormal} "fontsize =" {staticresource quota} "foreground =" {staticresource phoneforegroundbrush} "> <usercontrol. resources> <style X: Name = "mycustomtextboxstyle" targettype = "TK: phonetextbox "> <setter property =" width "value =" 375 "/> <setter property =" margin "value =" 0 "/> <setter property =" borderthickness "value = "0"/> <setter property = "padding" value = "-5"/> <setter property = "background" value = "Transparent"/> <setter property = "fontsize "value =" 24 "/> <setter property =" verticalalignment "value =" center "/> </style> <style X: name = "custompasswordboxstyle" targettype = "passwordbox"> <setter property = "fontsize" value = "22"/> <setter property = "background" value = "white"/> <setter property = "borderthickness" value = "0"/> <setter property = "verticalignment" value = "center"/> <setter property = "margin" value = "5 0 0 0 "/> </style> </usercontrol. resources> <grid X: Name = "layoutroot"> <TK: phonetextbox X: name = "showpwdtb" hint = "enter a number or letter" style = "{staticresource mycustomtextboxstyle}" visibility = "visible" textchanged = "onshowpwdchanged" gotfocus = "onshowpwdtbgotfocus"/> <passwordbox X: name = "hidepwdtb" style = "{staticresource custompasswordboxstyle}" lostfocus = "onhidepwdtblostfocus" padding = "8 0 0 0" visibility = "Collapsed"/> </GRID> </usercontrol>

. CS file code:

Public partial class passwordtextbox: usercontrol {# region dependency properties public static readonly dependencyproperty isshowpasswordproperty = dependencyproperty. register ("isshowpassword", typeof (bool), typeof (passwordtextbox), new propertymetadata (onisshowpasswordpropertychanged); public static readonly dependencyproperty passwordproperty = dependencyproperty. register ("password", typeof (St Ring), typeof (passwordtextbox), new propertymetadata (onpasswordpropertychanged); # endregion # region data members // when the password is empty, keep the showpwdtb visibility (used to display hint text ); the operation for setting the hidden password is invalid. Private bool m_isneedhidepassword; # endregion # region constructor public passwordtextbox () {initializecomponent (); m_isneedhidepassword = false ;} # endregion # Region Public Methods public bool isshowpassword {get {return (bool) getv Alue (isshowpasswordproperty);} set {setvalue (isshowpasswordproperty, value);} Public String password {get {If (showpwdtb. visibility = visibility. visible) {return showpwdtb. text;} else {return hidepwdtb. password ;}} set {setvalue (passwordproperty, value) ;}# endregion # region event handler private void onshowpwdchanged (Object sender, eventargs e) {If (showpwdtb. visibility = visibil Ity. visible & showpwdtb. text. length = 0) {showpwdtb. hint = appres. passwordhinttext;} else {showpwdtb. hint = string. empty ;}} private void onshowpwdtbgotfocus (Object sender, eventargs e) {If ((! Isshowpassword & showpwdtb. text. length = 0) | m_isneedhidepassword) {m_isneedhidepassword = false; hidepasswordtb (); hidepwdtb. focus () ;}} private void onhidepwdtblostfocus (Object sender, eventargs e) {If (hidepwdtb. password. length = 0) {showpasswordtb (); showpwdtb. TEXT = string. empty; showpwdtb. hint = appres. passwordhinttext;} Private Static void onisshowpasswordpropertychanged (dependencyobject D, dependencypropertychangedeventargs e) {passwordtextbox pwdtb = D as passwordtextbox; If (bool) E. newvalue) {pwdtb. showpwdtb. visibility = visibility. visible; pwdtb. hidepwdtb. visibility = visibility. collapsed; pwdtb. showpwdtb. TEXT = pwdtb. hidepwdtb. password; pwdtb. hidepwdtb. password = string. empty; pwdtb. m_isneedhidepassword = false;} else {If (pwdtb. showpwdtb. text. length = 0) {pwdtb. m_isneedhidepassword = true; return;} pwdtb. showpwdtb. visibility = visibility. collapsed; pwdtb. hidepwdtb. visibility = visibility. visible; pwdtb. hidepwdtb. password = pwdtb. showpwdtb. text; pwdtb. showpwdtb. TEXT = string. empty ;}} Private Static void onpasswordpropertychanged (dependencyobject D, dependencypropertychangedeventargs e) {passwordtextbox pwdtb = D as passwordtextbox; If (pwdtb. showpwdtb. visibility = visibility. visible) {pwdtb. showpwdtb. TEXT = (string) E. newvalue;} else {pwdtb. hidepwdtb. password = (string) E. newvalue ;}# endregion # region private methods private void showpasswordtb () {showpwdtb. visibility = visibility. visible; hidepwdtb. visibility = visibility. collapsed;} private void hidepasswordtb () {showpwdtb. visibility = visibility. collapsed; hidepwdtb. visibility = visibility. visible;} # endregion}

Take study notes only. Thank you for your reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.