Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:55:24+00:00 2026-05-30T17:55:24+00:00

I have created custom component for displaying text with either Simple or Password mode,

  • 0

I have created custom component for displaying text with either Simple or Password mode, intention to develop this control is the Silverlight does not support custom TextMode (like Password or Text).

This is my requirement
In addition to the access rights it will be possible for organizations to specify restricted access to certain fields in the database. The access restriction to these fields will be Update and Redacted, thus meaning if a specific field has true against Update then a user will be able to update the field as well as viewing it, and if the field has true against Redacted then the user will only be able to see a redacted value in the filed (possibly asterisks – * * * * *). It will be possible to set a field to being Update-able and Redacted, thus meaning a user will see the redacted view but still be able to go and update the field with a new value. Such a requirement is mostly used when holding sensitive information against a contact or information which could be used to discriminate against the contact.

I have created custom control for this requirement and it is working perfectly. i am able to set TextMode dynamically but i couldn’t able to get the original value in my ViewModel. (I am able to get original value in View but can’t in ViewModel)

If i am access the original value in View using following then it is work.

string s = UserName.Text;

but not getting this value in ViewModel it is giving me like **.

Following is the complete code for PasswordTextBox control.

namespace QSys.Library.Controls
{
    public partial class PasswordTextBox : TextBox
    {
        #region Variables
        private string text = string.Empty;
        private string passwordChar = "*";
        private int selectionLength = 0;
        #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 (Mode)GetValue(TextModeProperty); }
            set { SetValue(TextModeProperty, value); }
        }
        public static readonly DependencyProperty TextModeProperty = DependencyProperty.Register("TextMode", typeof(Mode), typeof(PasswordTextBox), new PropertyMetadata(default(Mode)));
        #endregion

        #region Constructors
        public PasswordTextBox()
        {
            this.Loaded += new RoutedEventHandler(PasswordTextBox_Loaded);
        }
        #endregion

        #region Event Handlers
        void PasswordTextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.TextMode == Mode.Password)
            {
                text = base.Text;
                this.TextChanged += new TextChangedEventHandler(PasswordTextBox_TextChanged);
                this.KeyDown += new KeyEventHandler(PasswordTextBox_KeyDown);
                this.SelectionChanged += new RoutedEventHandler(PasswordTextBox_SelectionChanged);
                DisplayMaskedCharacters();
            }
            this.Loaded -= PasswordTextBox_Loaded;
        }
        void PasswordTextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            selectionLength = this.SelectionLength;
        }
        public void PasswordTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (base.Text.Length >= text.Length)
                text += base.Text.Substring(text.Length);
            else
            {
                int cursorPosition = this.SelectionStart;
                selectionLength = (selectionLength > 1) ? selectionLength : 1;
                text = text.Remove(cursorPosition, selectionLength);
            }
            DisplayMaskedCharacters();
            selectionLength = 0;
        }
        public void PasswordTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            int cursorPosition = this.SelectionStart;
            // Handle Delete and Backspace Keys Appropriately
            if (e.Key == System.Windows.Input.Key.Back && cursorPosition > 0)
            {
                DeleteAt(cursorPosition);
            }
            else if (e.Key == System.Windows.Input.Key.Delete)
            {
                DeleteAt(cursorPosition);
            }
            else
            {
                if (selectionLength > 0) text = text.Remove(cursorPosition, selectionLength);
                base.Text = text;
                this.Select((cursorPosition > text.Length ? text.Length : cursorPosition), 0);
                DisplayMaskedCharacters();
            }
            selectionLength = 0;
        }
        #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);
        }
        private void DeleteAt(int position)
        {
            if (text.Length > position)
            {
                text = text.Remove(position, 1);
                base.Text = base.Text.Remove(position, 1);
            }
        }
        #endregion
    }
}

LoginView.xaml

<control:PasswordTextBox  x:Name="UserName" TabIndex="1" Grid.Row="1" TextMode="Password" Text="{Binding Path=LoginModelValue.UserName, Mode=TwoWay,ValidatesOnNotifyDataErrors=True, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="1" Width="200" Height="25" Validatevalue:UpdateSourceTriggerHelper.UpdateSourceTrigger="True"/>

LoginViewModel.cs

public class LoginViewModel : INotifyPropertyChanged, IRegionMemberLifetime
    {
public LoginModel LoginModelValue
        {
            get { return _LoginModelValue; }
            set
            {
                _LoginModelValue = value;
                OnPropertyChanged("LoginModelValue");
            }
        }
}

LoginModel.cs

namespace QSys.Model
{
    public class LoginModel : INotifyPropertyChanged
    {
        #region Variables
        private string _userName;
        private string _password;
        #endregion

        #region Constructor
        public LoginModel()
        {
        }
        #endregion

        #region Properties
        [CustomValidation(typeof(PasswordTextBox), "IsValidUserName")]
        [Required(ErrorMessage = "User Name is required")]
        [Display(Name = "UserName")]
        [StringLength(50)]
        //[RegularExpression(@"^[a-zA-Z\\0-9\\.\\,\\'\s]+$", ErrorMessage = "Please enter right format.")]
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged("UserName");
                ValidateProperty("UserName", value);
            }
        }
        [Required(ErrorMessage = "Password is required")]
        [Display(Name = "Password")]
        [StringLength(10)]
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged("Password");
                ValidateProperty("Password", value);
            }
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region Private Methods
        public bool IsValidObject()
        {
            ICollection<ValidationResult> results = new Collection<ValidationResult>();
            return Validator.TryValidateObject(this, new ValidationContext(this, null, null), results, true) && results.Count == 0;
        }
        public void ValidateProperty(string propertyName, object value)
        {
            Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = propertyName });
        }
        #endregion
    }
}

**I am looking for solution since two days without any luck.

Please help me if you have any solution, your comments or suggestion would be highly appreciated.**

Thanks

Imdadhusen

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:55:25+00:00Added an answer on May 30, 2026 at 5:55 pm

    I have resolved using following code.

    I am missing Mode=TwoWay in LoginView.xaml:

    <control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}">
    

    Thanks,
    Imdadhusen

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom component (NumberFormattedTextField) of JFormattedTextField. This is the Formatter I
I have created a custom component. I add a dynamic input text box to
I have created a custom tag that looks like this: def textField = {
I have created a Popup that is custom component extends Canvas i want the
I have created a custom control using VB.NET in Visual Studio 2008 that gives
I have created a custom component based on spark.components.HGroup and it works mostly as
I am having an issue where I have created a Custom Control and when
I'm using Joomla 1.5. I have created a custom component that pulls data out
I have created a Custom View component and my layout file consists of a
I have created a custom facelets component and I need to set some value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.