1

我有一个名为“SelectAllOnFocus”的附加属性。真/假值。

    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 样式的一部分。应用程序中的所有文本框都应接收此属性及其相关行为。

我难住了。有任何想法吗?

谢谢

4

1 回答 1

0

如果你调用 ((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();
    }
于 2012-09-07T14:40:34.420 回答