Silverlight 文本框覆盖
我正在尝试获取文本框按键事件来捕获退格键按下事件。 我通过添加一个覆盖文本框的类来实现这一点。 我不知道该怎么做是与用户控件中文本框所在的类进行通信。
当用户在文本框中键入...例如 abcd 或退格键时,我需要更新用户控件上的某些内容。 假设我想要显示文本框中有多少个字符的东西。 有人可以帮我吗? 这是我到目前为止所拥有的,
Option Strict On
Imports System.Text.RegularExpressions
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
Dim textbox As New MyTextBox() With {.Width = 300, .Height = 100}
LayoutRoot.Children.Add(textbox)
End Sub
End Class
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
MyBase.OnKeyDown(e)
If e.Key = Key.Back Then
e.Handled = True
MyBase.OnKeyDown(e)
ElseIf e.Key = Key.Delete Then
e.Handled = True
MyBase.OnKeyDown(e)
End If
End Sub
End Class
谢谢 香农
I'm trying to get a textbox keydown event to trap the backspace key down event. I have that working by adding a Class that overrides the textbox. What i don't know how to do is have that communicate with the class where the textbox is in the user control.
When a user types in the text box... say abcd or backspace, i need to update something on the usercontrol. let's just say i want to have something that displays how many characters are in the textbox. can someone help me with that. Here is what i have so far
Option Strict On
Imports System.Text.RegularExpressions
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
Dim textbox As New MyTextBox() With {.Width = 300, .Height = 100}
LayoutRoot.Children.Add(textbox)
End Sub
End Class
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
MyBase.OnKeyDown(e)
If e.Key = Key.Back Then
e.Handled = True
MyBase.OnKeyDown(e)
ElseIf e.Key = Key.Delete Then
e.Handled = True
MyBase.OnKeyDown(e)
End If
End Sub
End Class
thanks
shannon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要子类化
TextBox
来执行此操作。 相反,为 TextBox.TextChanged 事件就在您的UserControl
类中。 调用此函数时,事件的发送者应该是您的TextBox
。 然后您可以从中获取文本并执行您需要执行的操作。更新:根据评论,以下内容应该有效:(
我的 VB 有点生疏,因此事件处理程序语法可能不完全正确。)
基本思想是在文本更改时收到通知
UserControl
中的TextBox
。 这样您就可以根据需要修改UserControl
的其他部分。You shouldn't need to subclass
TextBox
to do this. Instead, add handler for the TextBox.TextChanged event right in yourUserControl
class. When this is called, the sender of the event should be yourTextBox
. You can then get the text from it and do what you need to do.Update: Based on the comment, the following should work:
(My VB is a bit rusty, so the event handler syntax might not be completely correct.)
The basic idea is to get notified when text changes in the
TextBox
within yourUserControl
. This way you can modify the other parts of theUserControl
as necessary.你可能想错了。 由于您使用的是 Silverlight,因此您可以通过绑定表达式获取此信息。 举个简单的例子,您说您想知道文本框中有多少个字符。 您可以使用以下 xaml 来实现此目的:
You may be thinking the wrong way. Since you're in Silverlight, you can get this information with binding expressions. Just as a simple example, you said you want to know how many characters are in a text box. You can achieve this with the following xaml: