WPF TabItem 失去焦点事件

发布于 2024-12-12 03:14:50 字数 1044 浏览 0 评论 0 原文

我的 tabItems 标题上有 TextBox。我使用 LostFocus 和 MouseDoubleClick 事件将文本设置到 TextBox。

<TabControl>
                <TabItem Width="50">
                    <TabItem.Header>
                        <TextBox Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>
                    </TabItem.Header>
                </TabItem>
</TabControl>

    private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        TextBox text_box = sender as TextBox;
        if (text_box == null) { return; }

        text_box.IsReadOnly = false;
        text_box.SelectAll();
    }

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox text_box = sender as TextBox;
        if (text_box == null) { return; }

        text_box.IsReadOnly = true;
    }

仅当您单击 TextBox 外部的 TabItem 标题区域或另一个 TabItem 时,才会发生 LostFocus 事件。 单击选项卡项内容区域不会触发失去焦点事件。

当用户单击文本框之外的任何区域时,如何使文本框失去焦点?

I have tabItems with TextBox on their headers. I use LostFocus and MouseDoubleClick events to set the text to the TextBox.

<TabControl>
                <TabItem Width="50">
                    <TabItem.Header>
                        <TextBox Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>
                    </TabItem.Header>
                </TabItem>
</TabControl>

    private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        TextBox text_box = sender as TextBox;
        if (text_box == null) { return; }

        text_box.IsReadOnly = false;
        text_box.SelectAll();
    }

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox text_box = sender as TextBox;
        if (text_box == null) { return; }

        text_box.IsReadOnly = true;
    }

LostFocus event happens if only you click on the TabItem header area outside the TextBox or on enother TabItem.
Clicking the tab item content area doesn't fire lost focus event.

How to make the TextBox to lose focus when user click any area outside the TextBox?

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

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

发布评论

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

评论(2

來不及說愛妳 2024-12-19 03:14:51

失去焦点,元素必须首先拥有焦点。也许另一种选择是在初始化元素时将元素焦点放在适当的位置,例如:

更改

<TextBox Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>

<TextBox x:Name="MyTextBox" Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>

并在构造函数中使用 FocusManager 设置焦点元素:

...
FocusManager.SetFocusedElement(MyTextBox.Parent, MyTextBox);
...

焦点概述 是一个很好的资源,区分键盘焦点和逻辑焦点也很重要!

To lose focus, an element must first have focus. Perhaps an alternative could be to give your element focus in an appropriate place when your elements are initialized, for example:

Change

<TextBox Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>

To

<TextBox x:Name="MyTextBox" Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>

And in your constructor use the FocusManager to set the focused element:

...
FocusManager.SetFocusedElement(MyTextBox.Parent, MyTextBox);
...

Focus Overview on MSDN is a good resource, it is also important to distinguish between keyboard focus and logical focus!

雪化雨蝶 2024-12-19 03:14:50

要失去焦点,换句话说,要在选项卡内容(目标)内部获得焦点:

  1. 目标的 Focusable 设置为 true
  2. 目标应该是可测试的。目标的背景不应为空。
  3. 将事件处理程序添加到 PreviewMouseDown 事件(注意:不是 MouseDown)以对鼠标单击做出反应。
    如果您除了 3 步,您的应用程序将仅对 TAB 键做出反应。

    ;
        
            
                <文本框 
                    文本=“文本”IsReadOnly=“真” 
                    LostFocus =“TextBox_LostFocus”
                    MouseDoubleClick="TextBox_MouseDoubleClick"/>
            
            
        
    
    
    
    私人无效Border_PreviewMouseDown(对象发送者,MouseButtonEventArgs e)
    {
        var uiElement = 发送者作为 UIElement;
        if (uiElement != null) uiElement.Focus();
    }
    

To lost Focus, in other word to get Focus at inside tab content(target):

  1. Focusable of the target is set as true
  2. The target should be hit testable. Background of the target should not be null.
  3. Add event handler to PreviewMouseDown event(NOTE: NOT MouseDown) to react to mouse click.
    If you except 3 step, you application will react only to TAB key.

    <TabControl>
        <TabItem Width="50">
            <TabItem.Header>
                <TextBox 
                    Text="text" IsReadOnly="True" 
                    LostFocus="TextBox_LostFocus"
                    MouseDoubleClick="TextBox_MouseDoubleClick"/>
            </TabItem.Header>
            <Border Focusable="True" Background="Transparent" PreviewMouseDown="Border_PreviewMouseDown"/>
        </TabItem>
    </TabControl>
    
    
    private void Border_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        var uiElement = sender as UIElement;
        if (uiElement != null) uiElement.Focus();
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文