当 Visible = False 时,TextBox TextChanged 事件不会触发?
我有一个绑定到数据源的文本框。文本框的 TextChanged 事件更新另一个文本框。
问题是,我不想显示第一个文本框,所以我将其 Visible 属性设置为 false。
但是,现在 TextChanged
事件不会触发!
我可以通过在表单加载时设置 Visible=True
, Left=-100000
来解决这个问题,但我想要一个正确的解决方案。
谁能提供解释吗?
I have a textbox bound to a datasource. The textbox's TextChanged event updates another textbox.
The problem is, I do not want the first textbox to show, so I set its Visible property to false.
However, now the TextChanged
event does not fire!
I can work around it by setting Visible=True
, Left=-100000
on form load, but I'd like a proper solution.
Can anyone offer an explanation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 FormLoad 事件中而不是在设计器中设置
textbox.Visible = false
。它与句柄创建有关。如果文本框在构造过程中不可见,则不会创建句柄。如果文本框在构造后变得不可见,则句柄将被创建并且事件将发生。请参阅此讨论 在 MSDN 上。
Set your
textbox.Visible = false
in the FormLoad event instead of in the designer. It has to do with handle creation. If the textbox is not visible during construction, then the handle is not created. If the textbox is made invisible after construction, then the handle will have been created and events will occur.See this discussion on MSDN.
已接受答案的另一种解决方案是在 Loaded 上设置 TextChanged 侦听器,这对我来说是一样的(至少在 Silverlight 中)并保持设计器视图应有的样子。
An alternative solution to the accepted answer is to set up the TextChanged listener on Loaded, this works for me just the same (in Silverlight at least) and keeps the designer view as it should be.
它是什么类型的数据源?它可能有一个您可以直接使用的事件,而不是使用文本框来侦听更新。
What type of datasource is it? It might have an event that you can use directly instead of using a textbox to listen for an update.
如果
Visible
等于 false,则不会呈现Control
。因此它将无法触发事件。相反,请将
style
设置为display:none
。您可以使用Attributes
集合以编程方式设置/取消设置:If
Visible
is equal to false then theControl
is not rendered. Therefore it will be unable to fire an event.Instead, set the
style
todisplay:none
. You can set/unset this programmatically using theAttributes
collection: