gpt4 book ai didi

c# - WPF TextBox 在 Tab 焦点上全选

转载 作者:太空狗 更新时间:2023-10-30 01:01:41 25 4
gpt4 key购买 nike

使用 Tab 键完成焦点时,我正在尝试选择所有文本。但我无法找到正确的解决方案。现在我正在使用 GotFocusEvent 但现在当我用鼠标单击时它会引发事件。

我现在使用的代码是:

EventManager.RegisterClassHandler(typeof(System.Windows.Controls.TextBox), System.Windows.Controls.TextBox.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText));


void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = sender as System.Windows.Controls.TextBox;
if (textBox != null)
if (!textBox.IsReadOnly)
textBox.SelectAll();
}

最佳答案

引用这个答案

Textbox SelectAll on tab but not mouse click

您可以修改为...

EventManager.RegisterClassHandler(typeof(System.Windows.Controls.TextBox), System.Windows.Controls.TextBox.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnGotKeyboardFocus));

void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var textBox = sender as System.Windows.Controls.TextBox;

if (textBox != null && !textBox.IsReadOnly && e.KeyboardDevice.IsKeyDown(Key.Tab))
textBox.SelectAll();
}

您还应该注意有关在 LostKeyboardFocus 上清除选择的详细信息

关于c# - WPF TextBox 在 Tab 焦点上全选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com