Treeview Textbox Focus
-
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.
-
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.
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
-
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
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.