Accueil

Keywords cloud

jquery ebx prototype getelementbyid math time put_line cout jmp drupal mainloop luhn linq cbind ivalueconverter ada.text_io sorted mysql_select_db xor md5 sql cat mysql_connect environment given nserror mail singleton filesystemobject xaml regex design window preg_replace eax preg_match tuple tkinter stringbuilder mysql_error readystate nslog email regexp map session_start shift innerhtml curl nsarray sed float chomp mysql_fetch_assoc nsdictionary media background push pop split je grep color file_get_contents mysql_query setsockopt prawn unless socket theme defun cursor createobject function_exists rem registry collect perform dbms_output wpf done strtolower onreadystatechange ecx substr urllib raw_input rm text-align random mov quicksort list wordpress dict edx display tolowercase boost mysql_fetch_array

Languages

C#C# :: UI & GraphicalAuto select text of TextBox via DependencyProperty flag

Blank Source Code Snippet

Posted by Webmaster - 2012-02-03 10:40:56 (2 views)
public class AutoSelectProperties
{
    /// <summary>
    /// AutoSelectText dependency property.
    /// </summary>
    public static readonly DependencyProperty AutoSelectTextProperty = DependencyProperty.RegisterAttached("AutoSelectText",
                                                                            typeof(Boolean),
                                                                            typeof(AutoSelectProperties),
                                                                            new PropertyMetadata(OnAutoSelectTextChanged));

    /// <summary>
    /// PreventAutoSelectText dependency property.
    /// </summary>
    public static readonly DependencyProperty PreventAutoSelectTextProperty = DependencyProperty.RegisterAttached("PreventAutoSelectText",
                                                                            typeof(Boolean),
                                                                            typeof(AutoSelectProperties),
                                                                            null);

    /// <summary>
    /// This method is called when the value of the AutoSelectText property
    /// is set from the xaml
    /// </summary>
    /// <param name="d">The content control on which the property is set.</param>
    /// <param name="e"></param>
    private static void OnAutoSelectTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //This works because of event bubbling. 
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement != null)
        {
            if ((bool)e.NewValue)
            {
                frameworkElement.GotKeyboardFocus += OnGotFocus;
                frameworkElement.GotMouseCapture += OnGotFocus;
            }
            else
            {
                frameworkElement.GotKeyboardFocus -= OnGotFocus;
                frameworkElement.GotMouseCapture -= OnGotFocus;
            }
        }
    }

    private static void OnGotFocus(object sender, RoutedEventArgs e)
    {
        //Since we are using routed events, the sender parameter will not be the textbox that currently has focus. 
        //It will the root level content control (Grid) which has the AutoSelectText attached property.
        //The FocusManager class is used to get a reference to the control that has the focus.
        //TextBox textBox = FocusManager.GetFocusedElement(sender as DependencyObject ) as TextBox;
        //if (textBox != null && !(bool)textBox.GetValue(PreventAutoSelectTextProperty))
        //    textBox.Select(0, textBox.Text.Length);
        if (!(e.Source is TextBox)) return;

        var text = e.Source as TextBox;
        //text.Select(0,text.Text.Length);
        text.SelectAll();
    }


    #region Dependency property Get/Set
    public static Boolean GetAutoSelectText(DependencyObject target)
    {
        return (Boolean)target.GetValue(AutoSelectTextProperty);
    }
    public static void SetAutoSelectText(DependencyObject target, Boolean value)
    {
        target.SetValue(AutoSelectTextProperty, value);
    }
    public static Boolean GetPreventAutoSelectText(DependencyObject target)
    {
        return (Boolean)target.GetValue(PreventAutoSelectTextProperty);
    }
    public static void SetPreventAutoSelectText(DependencyObject target, Boolean value)
    {
        target.SetValue(PreventAutoSelectTextProperty, value);
    }
    #endregion
}

Comments

Related Source Code Snippets