WPF – 单击文本框外部时删除焦点

我有一些文本框,我希望专注于与WPF应用程序的正常行为略有不同。 基本上,我希望他们的行为更像是网页上的文本框行为。 也就是说,如果我点击文本框外的任何地方,它将失去焦点。 这样做的最佳方法是什么?

如果答案是以编程方式移除焦点,那么在边界外检测Mouseclick的最佳方法是什么? 如果我点击的元素将成为焦点的新接收者,该怎么办?

而不是添加新的控件到窗口我认为你应该添加名称到你的网格 ,甚至点击窗口设置焦点在网格上这样的东西:

      

代码背后:

 private void window1_MouseDown(object sender, MouseButtonEventArgs e) { grid1.Focus(); } 

我认为,解决此问题的更好方法是将MouseDown事件处理程序添加到窗口,后面带有代码:

 private void window_MouseDown(object sender, MouseButtonEventArgs e) { Keyboard.ClearFocus(); } 

另一种对我有用的方法就是使用

Mouse.AddPreviewMouseDownOutsideCapturedElementHandler

例如,假设你有一个TextBlock,当点击它时,应该通过显示一个聚焦的TextBox来编辑。 然后,当用户在TextBox外部单击时,应该再次隐藏它。 以下是您可以这样做的方法:

 private void YourTextBlock_OnMouseDown(object sender, MouseButtonEventArgs e) { YourTextBox.Visibility = Visibility.Visible; YourTextBox.Focus(); CaptureMouse(); Mouse.AddPreviewMouseDownOutsideCapturedElementHandler(this, OnMouseDownOutsideElement); } private void OnMouseDownOutsideElement(object sender, MouseButtonEventArgs e) { Mouse.RemovePreviewMouseDownOutsideCapturedElementHandler(this, OnMouseDownOutsideElement); ReleaseMouseCapture(); YourTextBox.Visibility = Visibility.Hidden; } 

我不是100%肯定,但是如果你在容器元素(Grid,StackPanel等)上将Focusable设置为true,那么它应该将焦点从文本框中移开。

如果你点击了可以抓住焦点的元素,你就可以得到你需要的东西。 例如,如果你有一些面板,你可以处理面板的mouseClick事件以满足你的需求,或者使用Richard Szalay的建议。

为避免代码隐藏,您可以使用此行为

  public class ClearFocusOnClickBehavior : Behavior { protected override void OnAttached() { AssociatedObject.MouseDown += AssociatedObject_MouseDown; base.OnAttached(); } private static void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { Keyboard.ClearFocus(); } protected override void OnDetaching() { AssociatedObject.MouseDown -= AssociatedObject_MouseDown; } } 

在XAML中使用:

在文本框外面的任何元素上,您希望他清除焦点,单击添加:

     

你不能明确地放松控件的焦点

您可以将焦点设置为其他控件

 **txt.Focusable=true; label.focus(); Keyboard.Focus(txtPassword);** 

试试这个

我遇到了类似的问题,但是当我将文本框包裹在ScrollViewer控件周围时,所有文本框在单击texbox之外的任何位置时都会自动失去焦点。

您可以使用IsKeyboardFocusedChanged事件:

 myTextBox.IsKeyboardFocusedChanged += myTextBox_IsKeyboardFocusedChanged; private void SendFileCaptionTextBox_IsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e) { if (e.NewValue.ToString() == "True") { // it's focused } else { // it's not focused } } 

我已经在原生WPF应用程序中尝试了所选答案,但由于文本框没有失去焦点,因此没有触发文本框失去焦点。 以下解决方案为我工作。

将鼠标按下事件绑定到Window:

      

和事件是:

 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); } }