I want to track which character is deleted by the user through Delete or BackSpace Key.
I am handling TextBox_ChangedEvent of textbox.
Can I extract the deleted character from TextChangedEventArgs e.Changes and if yes How can I do that?
I want to restrict user to from deleting any characters from the TextBox. I want user can delete only two characters ( let's say "(" or ")" )
Please suggest.
Below you will find code for an attached property that can be used like this to prevent anything but "(" or ")" from being deleted from the TextBox, period.
<TextBox my:TextBoxRestriction.RestrictDeleteTo="()" ... />
This will correctly handle all mouse and keyboard updates, such as:
Because of this it is much more powerful than simply intercepting PreviewKeyDown.
This also disables deletion of anything byt "(" or ")" by assigning directly to the .Text property, so this will fail:
textBox.Text = "Good morning";
Because of this the TextBoxRestriction class also contains another attached property called UnrestrictedText which, when set, is able to update the Text property bypassing the restrictions. This can be set in code using TextBoxRestriction.SetUnrestrictedText
, or data-bound like this:
<TextBox my:TextBoxRestriction.RestrictDeleteTo="()"
my:TextBoxRestriction.UnrestrictedText="{Binding PropertyNameHere}" />
In the implementation below, UnrestrictedText only works when RestrictDeleteTo is also set. A full implementation could be made that registers the event handler whenever either property is set and saves the handler in a third attached property for later unregistration. But for your current needs that is probably unnecessary.
Here is the implementation as promised:
public class TextBoxRestriction : DependencyObject
{
// RestrictDeleteTo: Set this to the characters that may be deleted
public static string GetRestrictDeleteTo(DependencyObject obj) { return (string)obj.GetValue(RestrictDeleteToProperty); }
public static void SetRestrictDeleteTo(DependencyObject obj, string value) { obj.SetValue(RestrictDeleteToProperty, value); }
public static readonly DependencyProperty RestrictDeleteToProperty = DependencyProperty.RegisterAttached("RestrictDeleteTo", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
{
PropertyChangedCallback = (obj, e) =>
{
var box = (TextBox)obj;
box.TextChanged += (obj2, changeEvent) =>
{
var oldText = GetUnrestrictedText(box);
var allowedChars = GetRestrictDeleteTo(box);
if(box.Text==oldText || allowdChars==null) return;
foreach(var change in changeEvent.Changes)
if(change.RemovedLength>0)
{
string deleted = box.Text.Substring(change.Offset, change.RemovedLength);
if(deleted.Any(ch => !allowedChars.Contains(ch)))
box.Text = oldText;
}
SetUnrestrictedText(box, box.Text);
};
}
});
// UnrestrictedText: Bind or access this property to update the Text property bypassing all restrictions
public static string GetUnrestrictedText(DependencyObject obj) { return (string)obj.GetValue(UnrestrictedTextProperty); }
public static void SetUnrestrictedText(DependencyObject obj, string value) { obj.SetValue(UnrestrictedTextProperty, value); }
public static readonly DependencyProperty UnrestrictedTextProperty = DependencyProperty.RegisterAttached("UnrestrictedText", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
{
DefaultValue = "",
PropertyChangedCallback = (obj, e) =>
{
var box = (TextBox)obj;
box.Text = (string)e.NewValue;
}
});
}
How it works: When you set UnrestrictedText it sets Text and vice versa. The TextChanged handler checks to see if Text is different than UnrestrictedText. If so, it knows that Text has been updated by some other mechanism than setting UnrestrictedText so is scans the changes for an illegal delete. If one is found it sets Text back to the value still stored in UnrestrictedText, preventing the change.
An attached behaviour to handle it
public static class TextInputBehaviour
{
public static bool GetIsDeleteRestricted(DependencyObject obj)
{
return (bool)obj.GetValue(IsDeleteRestrictedProperty);
}
public static void SetIsDeleteRestricted(DependencyObject obj, bool value)
{
obj.SetValue(IsDeleteRestrictedProperty, value);
}
public static readonly DependencyProperty IsDeleteRestrictedProperty=DependencyProperty.RegisterAttached("IsDeleteRestricted", typeof(bool), typeof(TextInputBehaviour), new UIPropertyMetadata(false, OnIsDeleteRestrictedChanged));
}
private static void OnIsDeleteRestrictedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox textBox = (TextBox)sender;
bool isDeleteRestricted = (bool)(e.NewValue);
if (isDeleteRestricted)
textBox.PreviewKeyDown += RestrictDeleteKey;
else
textBox.PreviewKeyDown -= RestrictDeleteKey;
}
private static void RestrictDeleteKey(object sender, KeyEventArgs e)
{
e.Handled = (e.Key == Key.Delete);
}
Drop the behaviour in the resources section
Then in your textbox markup block, set the behaviour
<TextBox local:TextInputBehaviour.IsDeleteRestricted="True" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With