How to track which character is deleted in TextBox in WPF?

前端 未结 3 962
闹比i
闹比i 2021-01-05 07:19

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 dele

相关标签:
3条回答
  • 2021-01-05 07:40

    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:

    1. Use of the Delete key with multiple characters selected
    2. Use of the Backspace key
    3. Use of Ctrl-X to cut
    4. 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.

    0 讨论(0)
  • 2021-01-05 07:42

    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" />
    
    0 讨论(0)
  • 2021-01-05 07:50

    I don't know WPF but assuming that it's same as WinForms for this (seems probable). The only way I know of is that you actually keep the current text in a variable and on text change, if it's not a delete or backspace, you update that text, otherwise you use it compare what's changed and if that change should be allowed.

    Edit: Looking at TextChangedEventArgs.Changes it seems like the way I describe above might still be the way to go, but that you maybe could use the Changes to compare the texts more efficiently.

    You might already have thought about it, but otherwise, remember to handle cut and paste also (and that the user might be doing that with the mouse rather than the keyboard).

    0 讨论(0)
提交回复
热议问题