Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have created custom Textbox in Silverlight 4, MVVM and PRISM 4. The custom text box has dynamic behavior link it dynamically set TextMode to either Password or Text.
This is working perfect. ( if i am bind TextMode static)
HTML
<control:PasswordTextBox x:Name="customTextBox2" Width="100" Height="30" Grid.Row="4" Grid.Column="1" Text="{Binding Email}"  TextMode="Password"/>


This is giving me an error (if i am binding with dynamic)
HTML
<control:PasswordTextBox x:Name="customTextBox1" Width="100" Height="30" Grid.Row="4" Grid.Column="1" Text="{Binding Email}"  TextMode="{Binding WritingMode}"/>


following is my ViewModel code
C#
[Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class UserRightsViewModel : NotificationObject, IRegionMemberLifetime
    {
 private Mode _writingMode = Mode.Text;
public Mode WritingMode
        {
            get { return _writingMode; }
            set
            {
                _writingMode = value; RaisePropertyChanged("WritingMode");
            }
        }

[ImportingConstructor]
        public UserRightsViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
UserSecurity security = new UserSecurity();
            FormSecurity formSecurity = security.GetSecurityList("Admin");
formSecurity.WritingMode =  Mode.Password;
}
}
following is the enum
C#
namespace QSys.Library.Enums
{
    public enum Mode
    {
        Text,
        Password
    }
}

following code for Custom PasswordTextBox
C#
namespace QSys.Library.Controls
{
    public partial class PasswordTextBox : TextBox
    {
        #region Variables
        private string _Text = string.Empty;
        private string _PasswordChar = "*";
        private Mode _TextMode = Mode.Text;
        #endregion

        #region Properties
        /// <summary>
        /// The text associated with the control.
        /// </summary>
        public new string Text
        {
            get { return _Text; }
            set
            {
                _Text = value;
                DisplayMaskedCharacters();
            }
        }
        /// <summary>
        /// Indicates the character to display for password input.
        /// </summary>
        public string PasswordChar
        {
            get { return _PasswordChar; }
            set { _PasswordChar = value; }
        }
        /// <summary>
        /// Indicates the input text mode to display for either text or password.
        /// </summary>
        public Mode TextMode
        {
            get { return _TextMode; }
            set { _TextMode = value; }
        }
        #endregion

        #region Constructors
        public PasswordTextBox()
        {
            this.TextChanged += new TextChangedEventHandler(PasswordTextBox_TextChanged);
            this.KeyDown += new System.Windows.Input.KeyEventHandler(PasswordTextBox_KeyDown);
            this.Loaded += new RoutedEventHandler(PasswordTextBox_Loaded);
        }
        #endregion

        #region Event Handlers
        void PasswordTextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //this.TextChanged += ImmediateTextBox_TextChanged;
        }
        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.ToCharArray()[0], _Text.Length);
            this.Select((cursorPosition > _Text.Length ? _Text.Length : cursorPosition), 0);
        }
        #endregion

        #region Public Methods
        #endregion
    }
}

I am getting following error if i am binding with dynamically.
Set property 'QSys.Library.Controls.PasswordTextBox.TextMode' threw an exception. [Line: 40 Position: 144]

Your answer would be appreciated.
Thanks in advance.
Imdadhusen
Posted

1 solution

Hi All,
I resolved this issue using following code, I have changed PasswordTextBox class TextMode property.
C#
public Mode TextMode
{
    get { return _TextMode; }
    set { _TextMode = value; }
}

To
C#
public Mode TextMode
{
    get { return (Mode) GetValue(TextModeProperty); }
    set { SetValue(TextModeProperty, value); }
}
public static readonly DependencyProperty TextModeProperty =
DependencyProperty.Register(&quot;TextMode&quot;, typeof(Mode), typeof(PasswordTextBox), new PropertyMetadata(default(Mode)));


Specially thanks to chopikadze for helping me to fix this.
http://stackoverflow.com/questions/9410431/error-in-binding-custom-textbox-property[^]

Thanks all of you guys who searching and trying to help me.
Imdadhusen
 
Share this answer
 
Comments
Savalia Manoj M 24-Feb-12 1:17am    
Good Solution....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900