TextBox AttachedProperty选择所有文本不按预期工作?

时间:2012-09-07 14:24:36

标签: wpf textbox behavior attached-properties selectall

我有一个名为“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)发件人).SelectedText进行“观察”时,该值是正确的(意味着文本框中的任何内容都显示为selectedText)。
  5. 文本框本身没有变化。没有选择文字。
  6. 这是一般WPF风格的一部分。应用程序中的所有文本框都应该接收此属性及其相关行为。

    我很难过。有什么想法吗?

    由于

1 个答案:

答案 0 :(得分:0)

如果你打电话给((TextBox)发送者)会发生什么.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();
    }