在Silverlight 2中创建密码输入框

本文介绍了一种在Silverlight 2 Beta1中实现密码输入框的方法。由于原生控件不支持密码显示特性,作者ChrisPietschmann自行开发了一个PasswordTextBox控件,并提供了源代码。文中还列举了使用示例及遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Silverlight Beta2中,没有提供密码输入框控件,估计在正式版里应该提供吧。Chris Pietschmann自己写了一个,原文地址是:

http://pietschsoft.com/post/2008/03/PasswordTextBox-for-Silverlight-2-Beta-1.aspx

创建一个PasswordTextBox.cs类,代码如下:

/// Copyright2008ChrisPietschmann( http://pietschsoft.com )
/// ThisworkislicensedunderaCreativeCommonsAttribution3.0UnitedStatesLicense
/// http://creativecommons.org/licenses/by/3.0/us/
///
/// ThisisaPasswordTextBoxbuiltforusewithSilverlight2Beta1
/// Thereasonthiswasbuilt,isbecausethestandardTextBoxin
/// Silverlight2Beta1doesnothavePasswordsupport.
/// OriginalLink: http://pietschsoft.com/post/2008/03/PasswordTextBox-for-Silverlight-2-Beta-1.aspx
///

using System.Windows.Controls;

namespace SilverlightPasswordTextBox
{
public partial class PasswordTextBox:TextBox
{
public PasswordTextBox()
{
this .TextChanged += new TextChangedEventHandler(PasswordTextBox_TextChanged);
this .KeyDown += new System.Windows.Input.KeyEventHandler(PasswordTextBox_KeyDown);
}

#region EventHandlers

public void PasswordTextBox_TextChanged( object sender,TextChangedEventArgse)
{
if ( base .Text.Length >= _Text.Length)
_Text
+= base .Text.Substring(_Text.Length);
DisplayMaskedCharacters();
}

public void PasswordTextBox_KeyDown( object sender,System.Windows.Input.KeyEventArgse)
{
int cursorPosition = this .SelectionStart;
int selectionLength = this .SelectionLength;

// HandleDeleteandBackspaceKeysAppropriately
if (e.Key == System.Windows.Input.Key.Back
|| e.Key == System.Windows.Input.Key.Delete)
{
if (cursorPosition < _Text.Length)
_Text
= _Text.Remove(cursorPosition,(selectionLength > 0 ? selectionLength: 1 ));
}

base .Text = _Text;
this .Select((cursorPosition > _Text.Length ? _Text.Length:cursorPosition), 0 );
DisplayMaskedCharacters();
}

#endregion

#region PrivateMethods

private void DisplayMaskedCharacters()
{
int cursorPosition = this .SelectionStart;

// ThischangestheTextpropertyofthebaseTextBoxclasstodisplayallAsterisksinthecontrol
base .Text = new string (_PasswordChar,_Text.Length);

this .Select((cursorPosition > _Text.Length ? _Text.Length:cursorPosition), 0 );
}

#endregion

#region Properties

private string _Text = string .Empty;
/// <summary>
/// Thetextassociatedwiththecontrol.
/// </summary>
public new string Text
{
get { return _Text;}
set
{
_Text
= value;
DisplayMaskedCharacters();
}
}

private char _PasswordChar = ' * ' ;
/// <summary>
/// Indicatesthecharactertodisplayforpasswordinput.
/// </summary>
public char PasswordChar
{
get { return _PasswordChar;}
set {_PasswordChar = value;}
}

#endregion
}
}

使用方法:

< UserControl x:Class ="SilverlightApplication1.Page"
xmlns
="http://schemas.microsoft.com/client/2007"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myctl
="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1" Width ="600" Height ="480" >
< Grid x:Name ="LayoutRoot" Background ="White" >
< Canvas Canvas.Top ="20" >
< myctl:PasswordTextBox x:Name ="UserPassword" Width ="200" Height ="30" Canvas.Top ="40" Canvas.Left ="20" ></ myctl:PasswordTextBox >
</ Canvas >
</ Grid >
</ UserControl >

注意:要先加入名称空间,具体的值是:

clr-namespace:名称空间全名;assembly=程序集名称

通过使用,发现两个问题:

1,<myctl:PasswordTextBoxPasswordChar="" />设置会报错

2,Text="初始值"仍旧是明文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值