I have a Grid
, and when I'm walking by cells of it, I enter to cell, but whithout selecting text. It's annoying because, when I need to change a value, 开发者_如何学运维I need firstly use Backspace key.
What can I do to after entering to cell have selected content of that cell?
I would recommend the use of a behavior for this purpose. Below I have included a minimal implementation that provides the functionality I think you're looking for:
/// <summary>
/// <see cref="Behavior{T}"/> of <see cref="TextBox"/> for selecting all text when the text box is focused
/// </summary>
public class TextBoxSelectOnFocusBehavior : Behavior<TextBox>
{
private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (sender as TextBox);
if (textBox != null)
textBox.SelectAll();
}
/// <summary>
/// React to the behavior being attached to an object
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotFocus += AssociatedObject_GotFocus;
}
/// <summary>
/// React to the behavior being detached from an object
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
}
}
Hope this helps.
精彩评论