TextBox AttachedProperty用于选择所有未按预期工作的文本



我有一个名为"SelectAllOnFocus"的附加属性。true/false的值。

    public static class TextBoxProps
    {
        private static void MyTextBoxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                ((TextBox)sender).Text = string.Empty;
            }
        }
        public static void SetSelectAllOnFocus(DependencyObject dependencyObject, bool     selectAllOnFocus)
        {
            if (!ReferenceEquals(null, dependencyObject))
            {
                dependencyObject.SetValue(SelectAllOnFocus, selectAllOnFocus);
            }
    }
    public static bool GetSelectAllOnFocus(DependencyObject dependencyObject)
    {
        if (!ReferenceEquals(null, dependencyObject))
        {
            return (bool)dependencyObject.GetValue(SelectAllOnFocus);
        }
        else
        {
            return false;
        }
    }
    private static void OnSelectAllOnFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        bool selectAllOnFocus = (bool)e.NewValue == true;
        var theTextBox = d as TextBox;
        if (selectAllOnFocus && theTextBox != null)
        {
            theTextBox.PreviewMouseDown -= MyTextBoxMouseEnter; theTextBox.PreviewMouseDown += MyTextBoxMouseEnter;
        }
    }
    private static void MyTextBoxMouseEnter(object sender, MouseEventArgs e)
    {
        ((TextBox)sender).SelectAll();
        e.Handled = false;
    }

    public static readonly DependencyProperty SelectAllOnFocus
       = DependencyProperty.RegisterAttached("SelectAllOnFocus", typeof(bool), typeof(TextBoxEscapeProperty),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnSelectAllOnFocus)));
}

发生的情况如下:

  1. PreviewMouseDown事件被触发
  2. 调用MyTextBoxMouseEnter方法
  3. 调用SelectAll()方法
  4. 当我对((TextBox)sender).SelectedText进行"监视"时,值是正确的(意味着文本框中的任何内容都显示为SelectedText)
  5. 文本框本身保持不变。未选择任何文本

这是一般WPF样式的一部分。应用程序中的所有文本框都应该接收此属性及其关联行为。

我被难住了。有什么想法吗?

感谢

如果调用((TextBox)sender).UpdateLayout()会发生什么;是否在SelectAll命令之后立即执行?或者您可能需要将键盘焦点设置为文本框。

使用这样的东西可能是一个更好的选择,如果用鼠标或键盘选择文本框,它就会起作用。(您需要修改它来检查您的"SelectAllOnFocus"属性)

在您的App.xaml.cs 中

    protected override void OnStartup(StartupEventArgs e)
    {
        // Select the text in a TextBox when it receives focus.
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton));
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText));
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText));
        base.OnStartup(e);
    }
    void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e)
    {
        // Find the TextBox
        DependencyObject parent = e.OriginalSource as UIElement;
        while (parent != null && !(parent is TextBox))
            parent = VisualTreeHelper.GetParent(parent);
        if (parent != null)
        {
            var textBox = (TextBox)parent;
            if (!textBox.IsKeyboardFocusWithin)
            {
                // If the text box is not yet focused, give it the focus and
                // stop further processing of this click event.
                textBox.Focus();
                e.Handled = true;
            }
        }
    }
    void SelectAllText(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null)
            textBox.SelectAll();
    }

最新更新