Silverlight Beta2中,没有提供密码输入框控件,估计在正式版里应该提供吧。Chris Pietschmann自己写了一个,原文地址是: http://pietschsoft.com/post/2008/03/PasswordTextBox-for-Silverlight-2-Beta-1.aspx 创建一个PasswordTextBox.cs类,代码如下: /// copyright 2008 Chris Pietschmann (http://pietschsoft.com) /// This work is licensed under a Creative Commons Attribution 3.0 United States License /// http://creativecommons.org/licenses/by/3.0/us/ /// /// This is a Password TextBox built for use with Silverlight 2 Beta 1 /// The reason this was built,is because the standard TextBox in /// Silverlight 2 Beta 1 does not have Password support. /// Original Link: 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 Event Handlers public void PasswordTextBox_TextChanged(object sender,TextChangedEventArgs e) { if (base.Text.Length >= _Text.Length) _Text += base.Text.Substring(_Text.Length); displayMaskedCharacters(); } public void PasswordTextBox_KeyDown(object sender,System.Windows.Input.KeyEventArgs e) { int cursorPosition = this.SelectionStart; int selectionLength = this.SelectionLength; // Handle Delete and Backspace Keys Appropriately 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 Private Methods private void displayMaskedCharacters() { int cursorPosition = this.SelectionStart; // This changes the Text property of the base TextBox class to display all Asterisks in the control 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> /// The text associated with the control. /// </summary> public new string Text { get { return _Text; } set { _Text = value; displayMaskedCharacters(); } } private char _PasswordChar = '*'; /// <summary> /// Indicates the character to display for password input. /// </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:PasswordTextBox PasswordChar="" />设置会报错 2,Text="初始值"仍旧是明文
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。