Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Treeview Textbox Focus

Treeview Textbox Focus

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfcomquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have a Treeview that has a heirarchial structure but only a layer deep. The top layer has a field that I wanted editable and the child layer also has a field. I have set up my VMs to know when selected and in edit mode. Right now when they go into edit mode a Textbox appears. This is done by using both a textblock and textbox with the visibility bound to edit mode or the inverted depending. All works except I would like the Textbox to gain focus and select all of the text. Any ideas how I could accomplish this? [Edit] I did this from here[^]

    public static class FocusExtension
    {
    public static bool GetIsFocused(DependencyObject obj)
    {
    return (bool)obj.GetValue(IsFocusedProperty);
    }

    public static void SetIsFocused(DependencyObject obj, bool value)
    {
    obj.SetValue(IsFocusedProperty, value);
    }

    public static readonly DependencyProperty IsFocusedProperty =
    DependencyProperty.RegisterAttached(
    "IsFocused", typeof(bool), typeof(FocusExtension),
    new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

    private static void OnIsFocusedPropertyChanged(DependencyObject d,
    DependencyPropertyChangedEventArgs e)
    {
    var uie = (UIElement) d;
    if ((bool)e.NewValue)
    {
    uie.Focus(); // Don't care about false values.

         var txtBox = uie as TextBox;
         if (txtBox != null)
         {
              txtBox.SelectAll();
         }
     }
    

    }
    }

    then added the binding to the focus extension.

    extensions:FocusExtension.IsFocused="{Binding Path=IsFocused}"

    Seems to work :)

    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet. The interesting thing about software is it can not reproduce, until it can.

    W 1 Reply Last reply
    0
    • L Lost User

      I have a Treeview that has a heirarchial structure but only a layer deep. The top layer has a field that I wanted editable and the child layer also has a field. I have set up my VMs to know when selected and in edit mode. Right now when they go into edit mode a Textbox appears. This is done by using both a textblock and textbox with the visibility bound to edit mode or the inverted depending. All works except I would like the Textbox to gain focus and select all of the text. Any ideas how I could accomplish this? [Edit] I did this from here[^]

      public static class FocusExtension
      {
      public static bool GetIsFocused(DependencyObject obj)
      {
      return (bool)obj.GetValue(IsFocusedProperty);
      }

      public static void SetIsFocused(DependencyObject obj, bool value)
      {
      obj.SetValue(IsFocusedProperty, value);
      }

      public static readonly DependencyProperty IsFocusedProperty =
      DependencyProperty.RegisterAttached(
      "IsFocused", typeof(bool), typeof(FocusExtension),
      new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

      private static void OnIsFocusedPropertyChanged(DependencyObject d,
      DependencyPropertyChangedEventArgs e)
      {
      var uie = (UIElement) d;
      if ((bool)e.NewValue)
      {
      uie.Focus(); // Don't care about false values.

           var txtBox = uie as TextBox;
           if (txtBox != null)
           {
                txtBox.SelectAll();
           }
       }
      

      }
      }

      then added the binding to the focus extension.

      extensions:FocusExtension.IsFocused="{Binding Path=IsFocused}"

      Seems to work :)

      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet. The interesting thing about software is it can not reproduce, until it can.

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      I like that to occur when any text box is entered, and so I create a method in my App class that handles that :

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

      and then I register a handler for the textbox GotKeyboardFocusEvent in my OnStartUP method

      EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true);

      That way all textboxes select their text whenever they get focus. Hope this helps

      Everyone dies - but not everyone lives

      L 1 Reply Last reply
      0
      • W Wayne Gaylard

        I like that to occur when any text box is entered, and so I create a method in my App class that handles that :

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

        and then I register a handler for the textbox GotKeyboardFocusEvent in my OnStartUP method

        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true);

        That way all textboxes select their text whenever they get focus. Hope this helps

        Everyone dies - but not everyone lives

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Could work but I do not want all Textboxes to behave that way necessarily (I can't not mandate that behavior). I found a solution on SO which I edited in my OP. Thank you for you help!

        Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet. The interesting thing about software is it can not reproduce, until it can.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups