我有Grid
,当我走过它的细胞时,我进入细胞,但没有选择文字。这很烦人,因为当我需要更改一个值时,我首先需要使用Backspace键。
进入单元格后,如何选择该单元格的内容?
答案 0 :(得分:1)
我建议为此目的使用行为。下面我提供了一个最小的实现,它提供了我正在寻找的思考的功能:
/// <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;
}
}
希望这有帮助。