WPF TextBox选择Tab焦点上的所有内容

时间:2016-05-31 10:11:48

标签: c# .net wpf keyboard-events wpf-textbox

我正在尝试使用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();
}

2 个答案:

答案 0 :(得分:4)

参考这个答案

Textbox SelectAll on tab but not mouse click

您拥有的内容可以修改为......

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

您还应该注意有关在@RestController public class GreetingsController { @Autowired private SomeService svc; @RequestMapping("/greeting") public String greeting() { System.out.println(svc.sayHello()); return svc.sayHello(); } }

上清除选择的详细信息

答案 1 :(得分:0)

使用MouseButtonState,如下所示:

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