WPF - Remove focus when clicking outside of a textbox

前端 未结 13 2177
粉色の甜心
粉色の甜心 2020-12-02 19:11

I have some textboxes where I would like focus to behave a little differently than normal for a WPF application. Basically, I would like them to behave more like a textbox b

13条回答
  •  青春惊慌失措
    2020-12-02 19:26

    I have tried the selected answer in react native WPF application but it was not triggering lost focus of textbox due to which textbox was not losing the focus. Following solution work for me.

    Bound a mouse-down event to Window:

    
        
              
        
    
    

    and event is:

    private void window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        TextBox textBox = Keyboard.FocusedElement as TextBox;
        if (textBox != null)
        {
            TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
            textBox.MoveFocus(tRequest);
        }
    }
    

提交回复
热议问题