初始焦点和全选行为

Ber*_*ryl 6 wpf focus

我有一个嵌套在窗口内的用户控件,该窗口充当对话框显示的shell.我在shell窗口中忽略焦点,在托管用户控件中,我使用FocusManager将初始焦点设置为命名元素(文本框),如下所示.

这样做,将光标设置在命名文本框的开头; 但是我希望选择所有文本.

TextBoxSelectionBehavior类(下面)通常就是这样,但在这种情况下不是这样.是否有一个简单的xaml修复程序可以在初始焦点上选择指定文本框中的文本?

干杯,
Berryl

TextBox选择行为

// in app startup
TextBoxSelectionBehavior.RegisterTextboxSelectionBehavior();

/// <summary>
/// Helper to select all text in the text box on entry
/// </summary>
public static class TextBoxSelectionBehavior
{
    public static void RegisterTextboxSelectionBehavior()
    {
        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(OnTextBox_GotFocus));
    }

    private static void OnTextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        var tb = (sender as TextBox);
        if (tb != null)
            tb.SelectAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

托管的UserControl

<UserControl   
<DockPanel KeyboardNavigation.TabNavigation="Local" 
    FocusManager.FocusedElement="{Binding ElementName=tbLastName}" >

            <TextBox x:Name="tbLastName" ... />
Run Code Online (Sandbox Code Playgroud)

停止差距解决方案

根据下面的Rachel的评论,我放弃了FocusManger,支持一些代码:

tbLastName.Loaded += (sender, e) => tbLastName.Focus();
Run Code Online (Sandbox Code Playgroud)

尽管如此,仍然会喜欢一种简单而常见的苦差事的陈述方法......

Rac*_*hel 14

我通常使用一个AttachedProperty来使TextBox在焦点上突出显示它们的文本.它用得像

<TextBox local:HighlightTextOnFocus="True" />
Run Code Online (Sandbox Code Playgroud)

附属物的代码

public static readonly DependencyProperty HighlightTextOnFocusProperty =
    DependencyProperty.RegisterAttached("HighlightTextOnFocus", 
    typeof(bool), typeof(TextBoxProperties),
    new PropertyMetadata(false, HighlightTextOnFocusPropertyChanged));


[AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
[AttachedPropertyBrowsableForType(typeof(TextBox))]
public static bool GetHighlightTextOnFocus(DependencyObject obj)
{
    return (bool)obj.GetValue(HighlightTextOnFocusProperty);
}

public static void SetHighlightTextOnFocus(DependencyObject obj, bool value)
{
    obj.SetValue(HighlightTextOnFocusProperty, value);
}

private static void HighlightTextOnFocusPropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var sender = obj as UIElement;
    if (sender != null)
    {
        if ((bool)e.NewValue)
        {
            sender.GotKeyboardFocus += OnKeyboardFocusSelectText;
            sender.PreviewMouseLeftButtonDown += OnMouseLeftButtonDownSetFocus;
        }
        else
        {
            sender.GotKeyboardFocus -= OnKeyboardFocusSelectText;
            sender.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDownSetFocus;
        }
    }
}

private static void OnKeyboardFocusSelectText(
    object sender, KeyboardFocusChangedEventArgs e)
{
    var textBox = e.OriginalSource as TextBox;
    if (textBox != null)
    {
        textBox.SelectAll();
    }
}

private static void OnMouseLeftButtonDownSetFocus(
    object sender, MouseButtonEventArgs e)
{
    TextBox tb = FindAncestor<TextBox>((DependencyObject)e.OriginalSource);

    if (tb == null)
        return;

    if (!tb.IsKeyboardFocusWithin)
    {
        tb.Focus();
        e.Handled = true;
    }
}

static T FindAncestor<T>(DependencyObject current)
    where T : DependencyObject
{
    current = VisualTreeHelper.GetParent(current);

    while (current != null)
    {
        if (current is T)
        {
            return (T)current;
        }
        current = VisualTreeHelper.GetParent(current);
    };
    return null;
}
Run Code Online (Sandbox Code Playgroud)

编辑

基于下面的评论,有关刚刚摆脱什么的FocusManager.FocusedElement,并设置tb.Focus()tb.SelectAll()Loaded你的事件TextBox