如何确定导致 WPF TextBox 的 GotFocus 事件的原因 - 鼠标单击或 TAB 键?

发布于 2024-09-07 05:40:37 字数 158 浏览 4 评论 0原文

如何确定导致 WPF TextBox 的 GotFocus 事件的原因 - 鼠标单击或 TAB 键? 如果使用 TAB 键设置焦点,我需要更改边框颜色;如果使用鼠标单击设置焦点,则需要保留边框的标准颜色。因此,我需要从事件参数中提取导致事件的原因,或者(更好)编写触发器将其放入 TextBox 样式。

How to determine what caused GotFocus event of WPF TextBox - mouse click or TAB key?
I need to change border color if focus was set with TAB key and leave border's standart color if focus was set with mouse click. So I need to extract from event args what caused an event, or (better) write trigger to put it into TextBox style.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2024-09-14 05:40:38

我建议使用 OnKeyUp 和 OnMouseUp 事件而不是 GotFocus 事件来确定这一点。在 OnKeyUp 中,您需要测试(请参阅 Eventargs)是否按下了 Tab 键。

I would suggest using the OnKeyUp and OnMouseUp events rather than the GotFocus event to determine this. In OnKeyUp, you will need to test (see Eventargs) that it was the Tab key that was pressed.

千里故人稀 2024-09-14 05:40:38

您能否扩展 WPF TextBox,然后将其用于所有文本框?然后,您可以使用一些覆盖的事件来确定您的注意力集中方式,或进行边界更改。

class MySpecialTextBox : TextBox
{
    protected override void OnIsKeyboardFocusWithinChanged(System.Windows.DependencyPropertyChangedEventArgs e)
    {
        // Focused by keyboard
    }

    protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
    {
        // Focused by mouse
    }

    protected override void OnIsMouseCaptureWithinChanged(System.Windows.DependencyPropertyChangedEventArgs e)
    {
        // Focused by mouse
    }
}

Could you extend the WPF TextBox and then use that for all your text boxes instead? Then you could have some overridden events to determine how you were focused, or to do the border changes.

class MySpecialTextBox : TextBox
{
    protected override void OnIsKeyboardFocusWithinChanged(System.Windows.DependencyPropertyChangedEventArgs e)
    {
        // Focused by keyboard
    }

    protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
    {
        // Focused by mouse
    }

    protected override void OnIsMouseCaptureWithinChanged(System.Windows.DependencyPropertyChangedEventArgs e)
    {
        // Focused by mouse
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文