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:
- Use of the Delete key with multiple characters selected
- Use of the Backspace key
- Use of Ctrl-X to cut
- Clicking the "Cut" button on your menu bar
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.