如何响应WPF的ComboBox的输入事件?

m_halfman 2009-05-18 10:46:21
没有TextChanged事件。
TextInput事件不响应。
KeyUp事件只响应第一次输入。
...全文
993 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
capcom923 2011-06-27
  • 打赏
  • 举报
回复
我找到方法了:
第一种:如http://liuqi740924.blog.163.com/blog/static/803471542010834316688/
大哥所说

第二种:通过楼上的楼上的楼上的……所说的VisualTree,具体使用系统内置的VisualTreeHelper类

例如你想找this.cbStandards的TextBox控件,可如下索取
((TextBox)VisualTreeHelper.GetChild( (DependencyObject)(VisualTreeHelper.GetParent( (DependencyObject)this.cbStandards)),1))
你可以给这个东东加事件。
冰与火的交融 2011-02-11
  • 打赏
  • 举报
回复
我是要靠这样搞定的:(vb.net)


#Region "TextChanged"
Private Sub UserName_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles UserName.KeyUp
UserNameTextChanged()
End Sub
Private Sub UserName_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles UserName.LostFocus
UserNameTextChanged()
End Sub
Private Sub UserName_PreviewTextInput(ByVal sender As Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles UserName.PreviewTextInput
UserNameTextChanged()
End Sub
Private Sub UserNameTextChanged()
If UserName.Text Is Nothing Then
Else
If UserName.Text.Trim = String.Empty Then
ButtonRemove.Visibility = Windows.Visibility.Hidden
Else
For Each Item In UserName.Items
If Item = UserName.Text.Trim Then
ButtonRemove.Visibility = Windows.Visibility.Visible
Else
ButtonRemove.Visibility = Windows.Visibility.Hidden
End If
Next
End If
End If
End Sub
#End Region
冰与火的交融 2011-02-11
  • 打赏
  • 举报
回复
我是要靠这样搞定的:(vb.net)

#Region "TextChanged"
Private Sub UserName_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles UserName.KeyUp
UserNameTextChanged()
End Sub
Private Sub UserName_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles UserName.LostFocus
UserNameTextChanged()
End Sub
Private Sub UserName_PreviewTextInput(ByVal sender As Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles UserName.PreviewTextInput
UserNameTextChanged()
End Sub
Private Sub UserNameTextChanged()
If UserName.Text Is Nothing Then
Else
If UserName.Text.Trim = String.Empty Then
ButtonRemove.Visibility = Windows.Visibility.Hidden
Else
For Each Item In UserName.Items
If Item = UserName.Text.Trim Then
ButtonRemove.Visibility = Windows.Visibility.Visible
Else
ButtonRemove.Visibility = Windows.Visibility.Hidden
End If
Next
End If
End If
End Sub
#End Region
kissyou886 2009-05-19
  • 打赏
  • 举报
回复
可以试试OnPreviewTextInput事件,没有时间亲自试,帮顶
光宇广贞 2009-05-19
  • 打赏
  • 举报
回复
如果你装了VS的话,那么在microsoft sdks\windows\v6.0下面你可以找到这个xamlpad程序,
用这个来查看视觉树。

重写controltemplate可以看MSDN,很全。

combobox里面textbox 的name是PART_EditableTextBox
m_halfman 2009-05-19
  • 打赏
  • 举报
回复
视觉树怎么用?具体的写法有没有……
insuya 2009-05-19
  • 打赏
  • 举报
回复
通过Combobox的视觉树找到TextBox就可以了哇,TextBox的Name是PART_EditableTextBox
修改一下昵称 2009-05-19
  • 打赏
  • 举报
回复
up
光宇广贞 2009-05-19
  • 打赏
  • 举报
回复
哈哈……
终于碰上一个用WPF的了。

我告诉你一个最方便的方法。

你可以重写combobox的controltemplate

你用xamlpad查一下就知道

在combobox的视觉树里面,会有textbox,你响应它的textchanged事件就好了。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧