Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF Select All Focus Behavior

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
20 Jun 2018CPOL 27.1K   297   6   19
This behavior will select the entire text when a TextBox or PasswordBox gains focus

Introduction

I had a situation with a couple of PasswordBox controls, and it was frustrating because you almost never want to add to a password, especially since the actual text is obscured. I looked around for a solution. There were some custom TextBox controls to do this, but I really like to use a behavior when the impact is so minor on a control. I also found some interaction behaviors but I do not like to use these unless I have to, and I did find a behavior that only worked on TextBox controls. I therefore made my own behavior.

The Code

The following is the code for the behavior:

C#
public class SelectAllFocusBehavior
{
    public static bool GetEnable(FrameworkElement frameworkElement)
    {
        return (bool)frameworkElement.GetValue(EnableProperty);
    }

    public static void SetEnable(FrameworkElement frameworkElement, bool value)
    {
        frameworkElement.SetValue(EnableProperty, value);
    }

    public static readonly DependencyProperty EnableProperty =
             DependencyProperty.RegisterAttached("Enable",
                typeof(bool), typeof(SelectAllFocusBehavior),
                new FrameworkPropertyMetadata(false, OnEnableChanged));

    private static void OnEnableChanged
               (DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement == null) return;

        if (e.NewValue is bool == false) return;

        if ((bool)e.NewValue)
        {
            frameworkElement.GotFocus += SelectAll;
            frameworkElement.PreviewMouseDown += IgnoreMouseButton;
        }
        else
        {
            frameworkElement.GotFocus -= SelectAll;
            frameworkElement.PreviewMouseDown -= IgnoreMouseButton;
        }
    }

    private static void SelectAll(object sender, RoutedEventArgs e)
    {
        var frameworkElement = e.OriginalSource as FrameworkElement;
        if (frameworkElement is TextBox)
            ((TextBoxBase)frameworkElement).SelectAll();
        else if (frameworkElement is PasswordBox)
            ((PasswordBox)frameworkElement).SelectAll();
    }

    private static void IgnoreMouseButton
            (object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        var frameworkElement = sender as FrameworkElement;
        if (frameworkElement == null || frameworkElement.IsKeyboardFocusWithin) return;
        e.Handled = true;
        frameworkElement.Focus();
    }
}

Using the Code

This is how this behavior would be used within WPF XAML:

XML
<TextBox Width="150"
         Margin="20"
         local:SelectAllFocusBehavior.Enable="True"
         Text="This is with Behavior" />

History

  • 2018/06/20: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 823470512-Nov-20 3:48
Member 823470512-Nov-20 3:48 
QuestionA New GIg -- WPF :-( Pin
Clifford Nelson27-Jun-18 6:39
Clifford Nelson27-Jun-18 6:39 
QuestionAnother far simpler approach Pin
Graeme_Grant22-Jun-18 1:23
mvaGraeme_Grant22-Jun-18 1:23 
AnswerRe: Another far simpler approach Pin
Clifford Nelson22-Jun-18 4:12
Clifford Nelson22-Jun-18 4:12 
AnswerRe: Another far simpler approach Pin
George Swan22-Jun-18 5:54
mveGeorge Swan22-Jun-18 5:54 
GeneralRe: Another far simpler approach Pin
gersis7614-Aug-19 22:46
gersis7614-Aug-19 22:46 
QuestionAn Alternative Approach Pin
George Swan21-Jun-18 4:42
mveGeorge Swan21-Jun-18 4:42 
AnswerRe: An Alternative Approach Pin
Clifford Nelson22-Jun-18 4:13
Clifford Nelson22-Jun-18 4:13 
GeneralRe: An Alternative Approach Pin
George Swan22-Jun-18 4:31
mveGeorge Swan22-Jun-18 4:31 
AnswerRe: An Alternative Approach Pin
Clifford Nelson22-Jun-18 6:00
Clifford Nelson22-Jun-18 6:00 
AnswerRe: An Alternative Approach Pin
Clifford Nelson22-Jun-18 13:23
Clifford Nelson22-Jun-18 13:23 
GeneralRe: An Alternative Approach Pin
George Swan22-Jun-18 20:06
mveGeorge Swan22-Jun-18 20:06 
QuestionIts a shame no one wants to use WPF anymore Pin
Sacha Barber21-Jun-18 1:50
Sacha Barber21-Jun-18 1:50 
AnswerRe: Its a shame no one wants to use WPF anymore Pin
RugbyLeague21-Jun-18 2:26
RugbyLeague21-Jun-18 2:26 
AnswerRe: Its a shame no one wants to use WPF anymore Pin
michaeldjackson21-Jun-18 3:13
michaeldjackson21-Jun-18 3:13 
AnswerRe: Its a shame no one wants to use WPF anymore Pin
Pete O'Hanlon21-Jun-18 3:39
subeditorPete O'Hanlon21-Jun-18 3:39 
GeneralRe: Its a shame no one wants to use WPF anymore Pin
Sacha Barber21-Jun-18 18:58
Sacha Barber21-Jun-18 18:58 
AnswerSilverlight was what upset me Pin
Clifford Nelson22-Jun-18 4:17
Clifford Nelson22-Jun-18 4:17 
AnswerThere is UWP Pin
Clifford Nelson22-Jun-18 4:19
Clifford Nelson22-Jun-18 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.