Esempio n. 1
0
            public TextBoxBaseUserChangeTracker(TextBoxBase textBoxBase)
            {
                TextBoxBase = textBoxBase;
                LastText    = TextBoxBase.ToString();

                textBoxBase.PreviewTextInput += (s, e) =>
                {
                    IsTextInput = true;
                };

                textBoxBase.TextChanged += (s, e) =>
                {
                    var isUserChange = PressedKeys.Count > 0 || IsTextInput || LastText == TextBoxBase.ToString();
                    IsTextInput = false;
                    LastText    = TextBoxBase.ToString();
                    if (isUserChange)
                    {
                        UserTextChanged?.Invoke(this, e);
                    }
                };

                textBoxBase.PreviewKeyDown += (s, e) =>
                {
                    switch (e.Key)
                    {
                    case Key.Back:
                    case Key.Space:
                        if (!PressedKeys.Contains(e.Key))
                        {
                            PressedKeys.Add(e.Key);
                        }
                        break;
                    }
                    if (e.Key == Key.Back)
                    {
                        var textBox = textBoxBase as TextBox;
                        if (textBox.SelectionStart > 0 && textBox.SelectionLength > 0 && (textBox.SelectionStart + textBox.SelectionLength) == textBox.Text.Length)
                        {
                            textBox.SelectionStart--;
                            textBox.SelectionLength++;
                            e.Handled = true;
                            UserTextChanged?.Invoke(this, e);
                        }
                    }
                };

                textBoxBase.PreviewKeyUp += (s, e) =>
                {
                    if (PressedKeys.Contains(e.Key))
                    {
                        PressedKeys.Remove(e.Key);
                    }
                };

                textBoxBase.LostFocus += (s, e) =>
                {
                    PressedKeys.Clear();
                    IsTextInput = false;
                };
            }
Esempio n. 2
0
        public TextBoxBaseUserChangeTracker(TextBoxBase textBox)
        {
            TextBox  = textBox;
            LastText = TextBox.ToString();

            textBox.PreviewTextInput += (s, e) =>
            {
                IsTextInput = true;
            };

            textBox.TextChanged += (s, e) =>
            {
                var isUserChange = PressedKeys.Count > 0 || IsTextInput || LastText == TextBox.ToString();
                IsTextInput = false;
                LastText    = TextBox.ToString();
                if (isUserChange)
                {
                    UserTextChanged?.Invoke(this, e);
                }
            };

            textBox.PreviewKeyDown += (s, e) =>
            {
                switch (e.Key)
                {
                case Key.Back:
                case Key.Space:
                case Key.Delete:
                    if (!PressedKeys.Contains(e.Key))
                    {
                        PressedKeys.Add(e.Key);
                    }
                    break;
                }
            };

            textBox.PreviewKeyUp += (s, e) =>
            {
                if (PressedKeys.Contains(e.Key))
                {
                    PressedKeys.Remove(e.Key);
                }
            };

            textBox.LostFocus += (s, e) =>
            {
                PressedKeys.Clear();
                IsTextInput = false;
            };
        }