Wpf 取消选择组合框中的文本

Wpf 取消选择组合框中的文本,wpf,vb.net,combobox,attached-properties,Wpf,Vb.net,Combobox,Attached Properties,我有一个可编辑的组合框,设置为True,因此我可以在组合框中键入文本框。现在,我希望DropDownList打开,因此我为它创建了一个附加属性: Public Shared Function GetShowDropDown(obj As DependencyObject) As Boolean Return obj.GetValue(IsDigitOnlyProperty) End Function Public Shared Sub SetShowDropDown(obj As De

我有一个可编辑的
组合框
,设置为
True
,因此我可以在
组合框中键入
文本框
。现在,我希望DropDownList打开,因此我为它创建了一个
附加属性

Public Shared Function GetShowDropDown(obj As DependencyObject) As Boolean
    Return obj.GetValue(IsDigitOnlyProperty)
End Function

Public Shared Sub SetShowDropDown(obj As DependencyObject, value As Boolean)
    obj.SetValue(IsDigitOnlyProperty, value)
End Sub

Public Shared ReadOnly ShowDropDownProperty As DependencyProperty = DependencyProperty.RegisterAttached("ShowDropDown", GetType(Boolean), GetType(ControlBehaviour), New UIPropertyMetadata(False, AddressOf OnShowDropDown))

Private Shared Sub OnShowDropDown(sender As Object, e As DependencyPropertyChangedEventArgs)
    Dim comboBox As ComboBox = sender
    Dim showDropDown As Boolean = e.NewValue

    If showDropDown Then
        AddHandler comboBox.PreviewKeyUp, AddressOf DoShowDropDown
    Else
        RemoveHandler comboBox.PreviewKeyUp, AddressOf DoShowDropDown
    End If
End Sub

Private Shared Sub DoShowDropDown(sender As Object, e As KeyEventArgs)
    Dim comboBox As ComboBox = sender
    If comboBox.Text.Length > 0 Then
        comboBox.IsDropDownOpen = True
    Else
        comboBox.IsDropDownOpen = False
    End If
End Sub
每当我在
组合框中键入时,DropDownList就会打开,但有一个缺陷。让我这样解释:

  • 列表中有几个项目(字符串),其中1个是“测试”
  • 如果我在
    组合框的
    文本框中键入“T”,则在“T”后面添加“est”以显示“Test”
  • 现在,“测试”显示在
    文本框中,并被选中。因此,当我想键入“e”时,“Test”被删除,只有“e”显示在
    文本框中(如果选择了某个内容,则键入时的正常行为)
我不希望出现最后一个项目,如果我在
文本框中键入
,则可能无法选择任何内容,以便我可以继续键入“Test”。这是我按下字母“t”时的屏幕截图。我希望这可以解释我的问题

我认为应该发生的是,我必须以某种方式取消选择测试,或者将其设置为从
文本框中的第二个字符开始选择,但我不知道如何执行此操作



要添加其他内容,当我按Enter键时,我希望它隐藏下拉列表。

设置组合框的此属性

IsTextSearchEnabled="True"
解释 我们将使用
SelectedIndex
属性执行此操作。我们将选择-1索引。据了解,不能将任何项目设置为-1,因此不会选择任何项目。无论您说什么,项目的编号或列表都从0开始

但是,假设您有5项,您不能给它数字6,它将显示一个运行时错误

对于您的额外帮助,您不希望所有内容都消失,而只希望e出现,对吗?为此,您可以将
combobox
的所有文本保存在变量或
textbox
中,然后使用稍后输入的文本将其带回
combobox

我们将在计时器事件中执行第二个要求,将间隔设置为1毫秒,这样它可以更频繁地记录更改。您可以在任何需要的事件中执行此操作(
TextChanged
等)

代码和示例 对于不取代现有案文的案文:

Private Sub Form1_Load () Handles Mybase.Load
       Timer1.Interval = 1
End Sub

Private Sub Timer1_Tick () Handles Timer1.Tick
       Textbox1.Text = Combobox.Text
End Sub
将现在键入的文本指定为另一个变量,我们将编写以下代码:

Combobox.Text = Textbox1.Text & a 'assuming 'a' is the text you entered later.
我希望它将帮助你,并完美地工作

Combobox.Text = Textbox1.Text & a 'assuming 'a' is the text you entered later.