Click here to Skip to main content
13,706,648 members
Click here to Skip to main content
Add your own
alternative version

Stats

2.2K views
32 downloads
5 bookmarked
Posted 20 Jun 2018
Licenced CPOL

WPF Select All Focus Behavior

, 20 Jun 2018
Rate this:
Please Sign up or sign in to vote.
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:

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:

<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)

Share

About the Author

Clifford Nelson
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.

You may also be interested in...

Comments and Discussions

 
QuestionA New GIg -- WPF :-( Pin
Clifford Nelson27-Jun-18 6:39
mvpClifford Nelson27-Jun-18 6:39 
QuestionAnother far simpler approach Pin
Graeme_Grant22-Jun-18 1:23
mvpGraeme_Grant22-Jun-18 1:23 
AnswerRe: Another far simpler approach Pin
Clifford Nelson22-Jun-18 4:12
mvpClifford Nelson22-Jun-18 4:12 
AnswerRe: Another far simpler approach Pin
George Swan22-Jun-18 5:54
memberGeorge Swan22-Jun-18 5:54 
QuestionAn Alternative Approach Pin
George Swan21-Jun-18 4:42
memberGeorge Swan21-Jun-18 4:42 
AnswerRe: An Alternative Approach Pin
Clifford Nelson22-Jun-18 4:13
mvpClifford Nelson22-Jun-18 4:13 
GeneralRe: An Alternative Approach Pin
George Swan22-Jun-18 4:31
memberGeorge Swan22-Jun-18 4:31 
AnswerRe: An Alternative Approach Pin
Clifford Nelson22-Jun-18 6:00
mvpClifford Nelson22-Jun-18 6:00 
AnswerRe: An Alternative Approach Pin
Clifford Nelson22-Jun-18 13:23
mvpClifford Nelson22-Jun-18 13:23 
GeneralRe: An Alternative Approach Pin
George Swan22-Jun-18 20:06
memberGeorge Swan22-Jun-18 20:06 
QuestionIts a shame no one wants to use WPF anymore Pin
Sacha Barber21-Jun-18 1:50
mvpSacha Barber21-Jun-18 1:50 
AnswerRe: Its a shame no one wants to use WPF anymore Pin
RugbyLeague21-Jun-18 2:26
memberRugbyLeague21-Jun-18 2:26 
AnswerRe: Its a shame no one wants to use WPF anymore Pin
michaeldjackson21-Jun-18 3:13
membermichaeldjackson21-Jun-18 3:13 
AnswerRe: Its a shame no one wants to use WPF anymore Pin
Pete O'Hanlon21-Jun-18 3:39
protectorPete O'Hanlon21-Jun-18 3:39 
GeneralRe: Its a shame no one wants to use WPF anymore Pin
Sacha Barber21-Jun-18 18:58
mvpSacha Barber21-Jun-18 18:58 
AnswerSilverlight was what upset me Pin
Clifford Nelson22-Jun-18 4:17
mvpClifford Nelson22-Jun-18 4:17 
AnswerThere is UWP Pin
Clifford Nelson22-Jun-18 4:19
mvpClifford 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 20 Jun 2018
Article Copyright 2018 by Clifford Nelson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid