在 wpf 中启用禁用的文本框
是否可以有一个默认情况下禁用的文本框,但当用户双击它时启用它?
Is it possible to have a TextBox that is disabled by default, but becomes enabled when a user double-clicks it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将 TextBox 放置在 StackPanel 中,如下所示:
然后在 StackPanel 事件处理程序中:
您还可以按照此问题所述模拟 StackPanel DoubleClick:
带有 Click 和 DoubleClick 的 WPF StackPanel
you can place your TextBox inside StackPanel like this:
Then in StackPanel event handler:
you can also simulate StackPanel DoubleClick as described on this question:
WPF StackPanel with Click AND DoubleClick
这是非常不寻常的,而且当控件被禁用时,预计不会获得输入。用户看到禁用的控件通常甚至不会尝试单击/双击它。
也许您可以添加一个复选框来启用它(或属于它的功能),或者在不允许/不应该这样做时双击它时显示一个消息框。在这种情况下,您还可以明确添加无法双击的原因。
我之前看到的是控件前面没有文本的复选框。当您单击该复选框时,它会启用其后的控件(在您的情况下为文本框)。您甚至可以使用复选框的工具提示来提供复选框正在执行的操作的帮助信息。
That's very unusual, also when a control is disabled it is not expected to get input. Users seeing a disabled control normally would not even try to click/double click on it.
Maybe you can add a check box to enable it (or the function belonging to it), or show a message box when double clicking it when it is not allowed/meant to. In this case you also can clearly add a reason why it cannot be double clicked.
What I have seen before is a checkbox without text right before the control. When you click the checkbox it enables the control (text box in your case) after it. You can even use a tooltip for the check box to provide help information what the checkbox is doing.
我会尝试附加到 PreviewMouseDown 事件并在那里启用/禁用。
否则,您将不得不使用旧的 VB6 技巧,即在文本框上方放置一个透明控件来接收单击事件。
I would try attaching to the PreviewMouseDown event and enable/disable there.
Otherwise you will have to do the old VB6 trick of having a transparent control above the textbox to receive the click event.
这个问题很老了,但也许我可以帮助找到解决方案的人。
在最近的一个项目中,我需要模拟两种状态:查看和编辑。我已经使用文本框做到了这一点。在视图状态下,会显示值,但单击控件无法获得焦点。要启用编辑模式,您需要双击该控件。为了避免控件通过单击获得焦点以及禁用它的缺点,我使用了两个预览事件来控制文本框的行为并使其响应适应应用程序需求和状态。其中一个事件是 PreviewMouseDown:
在此事件中,如果文本框尚未获得焦点,我们将阻止鼠标按下按钮。这会阻止文本框获得焦点。所以它的行为就像一个标签。当控件获得焦点时,该事件不会被阻止,而是会向控件传播。请注意,也许您需要更改光标,因为当鼠标悬停在控件上时使用编辑光标。另请注意,我们仅阻止左侧按钮。
第二个事件如下所示:
在第二个事件中,如果尚未获得焦点,我们将通过双击左键将焦点移至控件。如果控件具有焦点,那么我们将让事件传播并且控件将正常运行。
就我而言,我为文本框创建了一种特殊的样式,保留了边框、背景和所有样式行为。这是 XAML 代码:
This question is old, but maybe I can help someone that finds a solution for this.
In a recent project I've needed to simulate two states: view and edit. I've do this using a textbox. In view state, value is displayed but you cannot got focus clicking on the control. To enable editing mode you need to double click the control. To avoid the control getting focus by clicking on it and also the disadvantages of disable it, I've used two preview events for control the behaviour of the textbox and adapt it's responses to application needs and state. One of the events is PreviewMouseDown:
In this event we will block the mouse down button if our textbox isn't focused yet. This prevents textbox to get focus. So it will behave like a label. When the control is focused, this event isn't blocked and is propagated towards the control. Note that maybe you need to change the cursor because editing cursor is used when the mouse is over the control. Note also, that we are blocking only the left button.
The second event looks like:
In the second event we will bring the focus to the control on double left click if it isn't focused yet. If control has focus so we will let the event propagate and the control will behave normally.
In my case I've created a special style for the textbox leaving borders, backgrounds, and all style behaviours. This is the XAML code: