-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextBoxMask cursor at beginning if empty #757
Changes from 4 commits
0067bdf
7981b2e
37a0f4e
3a4afce
a72e16c
662a47b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ private static void InitTextBoxMask(DependencyObject d, DependencyPropertyChange | |
textbox.TextChanging -= Textbox_TextChanging; | ||
textbox.Paste -= Textbox_Paste; | ||
textbox.Loaded -= Textbox_Loaded; | ||
textbox.GotFocus -= Textbox_GotFocus; | ||
textbox.Loaded += Textbox_Loaded; | ||
} | ||
|
||
|
@@ -119,10 +120,38 @@ private static void Textbox_Loaded(object sender, RoutedEventArgs e) | |
textbox.TextChanging += Textbox_TextChanging; | ||
textbox.SelectionChanged += Textbox_SelectionChanged; | ||
textbox.Paste += Textbox_Paste; | ||
textbox.GotFocus += Textbox_GotFocus; | ||
textbox.SetValue(OldTextProperty, textbox.Text); | ||
textbox.SelectionStart = 0; | ||
} | ||
|
||
private static void Textbox_GotFocus(object sender, RoutedEventArgs e) | ||
{ | ||
var textbox = sender as TextBox; | ||
var mask = textbox?.GetValue(MaskProperty) as string; | ||
var placeHolderValue = textbox?.GetValue(PlaceHolderProperty) as string; | ||
var representationDictionary = textbox?.GetValue(RepresentationDictionaryProperty) as Dictionary<char, string>; | ||
if (string.IsNullOrWhiteSpace(mask) || | ||
representationDictionary == null || | ||
string.IsNullOrEmpty(placeHolderValue)) | ||
{ | ||
return; | ||
} | ||
|
||
var placeHolder = placeHolderValue[0]; | ||
var displayText = mask; | ||
foreach (var key in representationDictionary.Keys) | ||
{ | ||
displayText = displayText.Replace(key, placeHolder); | ||
} | ||
|
||
// if the textbox got focus and the textbox is empty (contains only mask) set the textbox cursor at the beginning to simulate normal TextBox behavior if it is empty. | ||
if (string.Equals(textbox.Text, displayText)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably just always put the cursor to the end of where the user has entered text. If they have entered no text, it's at 0. If they have entered 3 of 10 characters (eg: phone number) then it's at 3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, for the phone number example it would be at 4 to bypass the first dash There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @IbraheemOsama any thoughts on this ? |
||
{ | ||
textbox.SelectionStart = 0; | ||
} | ||
} | ||
|
||
private static async void Textbox_Paste(object sender, TextControlPasteEventArgs e) | ||
{ | ||
e.Handled = true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest doing a single
if (textbox == null) return;
after this instead of all these null checks on every reference totextbox
.Having said that, this is a private method only used with the
TextBox.GotFocus
event, so thesender
should always be set with the current instance of the TextBox control!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Do explicit cast and remove ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I need to ask @deltakosh is there is any chance I can get a null here from the framework ?