停止WPF文本框失去焦点 [英] Stop a WPF Textbox from losing focus

查看:336
本文介绍了停止WPF文本框失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF 文本框,我想检查文本值是否正确,然后才允许它丢失键盘/焦点。 b

我曾尝试在 InputBox_LostFocus & InputBox_LostKeyboardFocus 事件,但它似乎并没有达到我想要的。



有关如何锁定焦点的任何建议到文本框

解决方案

您可以调用 Mouse.Capture UIElement 上。然后,这会给你所有的鼠标事件,无论是否元素。但使用起来很棘手。你可以捕捉你的文本框中的鼠标,并注册丢失的捕获事件,当你失去捕获,你可以夺回。你必须注意奇怪的行为。一般来说,它的不好的做法(我认为)不允许用户移出一个领域。最好是允许他们做任何他们想做的事情,但是在输入数据之后禁用他们按下的按钮,直到所有的字段都是有效的(或者类似的)

这里是一些链接

其他SO问题



msdn示例代码



组合框使用鼠标捕捉来判断用户是否点击了应用程序中的其他位置以关闭如果你点击另一个控件(或窗口外),它的组合框将打开。



我不知道这种技术是否会阻止你去掉元素。在wpf应用程序中有两种焦点。你有逻辑焦点和键盘焦点。多个元素可以同时具有逻辑焦点(每个元素都在焦点范围内)。想想例如一个文本框可以有逻辑的焦点,而你点击一个菜单(也有逻辑重点)。键盘焦点一次只能在一个地方。你会为自己做很多工作。我会认真考虑你是否以正确的方式进行互动。您可能需要花费数天的时间才能获得正确的互动如果您停止文本框失去焦点,如果用户单击关闭按钮会发生什么情况?

继承人 msdn文章焦点


I have a WPF Textbox, that I want to check that the text value is correct before I allow it to lose keyboard/focus.

I have tried setting e.Handled in the InputBox_LostFocus & InputBox_LostKeyboardFocus events, but it doesnt seem to be achieving what I want.

Any suggestions on how I can lock focus to a Textbox?

解决方案

You can call Mouse.Capture on a UIElement. This will then give you every mouse event that hapens whether on the element or not. but its tricky to use. You can capture the mouse on your text box and register for lost capture events, when you lose capture you can recapture. you have to watch out for strange behaviors. Generally its bad practice (I think) to not allow a user to move off a field. what is better is to allow them to do whatever they want, but disable the button that they push after entering data until all fields are valid (or something similar)

Here are some links

other SO question

msdn sample code

the combo box uses mouse capture to tell if the user has clicked elsewhere in the app to close the combo box if its open if you click on another control (or outside the window)

I dont know if this technique will stop you tabbing off the element. there are two kinds of focus in a wpf app. You have logical focus and keyboard focus. Multiple elements can have logical focus at once (each within a focus scope). think for example a textbox can have logical focus while you are clicking a menu (which has logical focus as well). Keyboard focus can only be in one place at a time. You are going to make a lot of work for yourself. I would seriously consider if you are doing your interaction in the right way. You could spend days getting this interaction correct. If you stop your textbox losing focus, what happens if the user clicks the close button?

heres the msdn article on focus

这篇关于停止WPF文本框失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆