输入3个字符后如何在autocompletebox中建议?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了输入3个字符后如何在autocompletebox中建议?相关的知识,希望对你有一定的参考价值。
我使用telerik autocompletetextbox来建议匹配的单词。但我的要求是在输入超过2个字符时开始建议。有人可以帮我解决这个问题吗?
代码i遵循:
<telerik:RadAutoCompleteBox x:Name="acbCustomerCd" Width="203" Height="30" ItemsSource="{Binding}" DisplayMemberPath="AccountNum" TextSearchPath="AccountNum" AutoCompleteMode="Suggest" TextSearchMode="Contains" Margin="1,0,5,0" BorderBrush="#ffcccccc" SelectionMode="Single" TabIndex="109" KeyboardNavigation.TabNavigation="Local"/>
在代码背后,
acbCustomerCd.ItemsSource = dtCustomerCd.DefaultView
答案
为了实现这一点,您必须创建一个派生自FilteringBehavior类的自定义类,覆盖FindMatchingItems并仅在搜索字符串超过3个字符时返回项目。在此之后,您可以在xaml中初始化新类并将其分配给RadAutoCompleteBox。
<Window.Resources>
<telerik:CustomFilteringBehavior x:Key="CustomFilteringBehavior" />
</Window.Resources>
...
<telerik:RadAutoCompleteBox ItemsSource="{Binding Items}" DisplayMemberPath="Name" FileringBehavior="{StaticResource CustomFilteringBehavior}" />
另一答案
我不知道telerik是什么,但是如果你想运行一个自动完成的过程,你可以这样做。
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Namespace WindowsFormsApplication4
Public Class Form1
Inherits Form
Public Sub New()
MyBase.New
InitializeComponent
Me.initializeFields
Me.loadCustomerFromNorthwindDbToDgv1
End Sub
Private CustomerId As DataGridViewTextBoxColumn
Private CompanyName As DataGridViewTextBoxColumn
Private Address As DataGridViewTextBoxColumn
Private Sub initializeFields()
Me.CustomerId = New DataGridViewTextBoxColumn
Me.CustomerId.Name = "CustomerID"
Me.CustomerId.DataPropertyName = "CustomerID"
Me.dataGridView1.Columns.Add(Me.CustomerId)
Me.CompanyName = New DataGridViewTextBoxColumn
Me.CompanyName.Name = "CompanyName"
Me.CompanyName.DataPropertyName = "CompanyName"
Me.dataGridView1.Columns.Add(Me.CompanyName)
Me.Address = New DataGridViewTextBoxColumn
Me.Address.Name = "Address"
Me.Address.DataPropertyName = "Address"
Me.dataGridView1.Columns.Add(Me.Address)
End Sub
Private dv As DataView
Private Sub loadCustomerFromNorthwindDbToDgv1()
Dim conConnect As SqlConnection = New SqlConnection("Data Source = EXCEL-PC; Database = 'Northwind.MDF'; Integrated Security = true")
Try
Dim dAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName, Address FROM Customers ", conConnect)
Dim DS As DataSet = New DataSet
Dim bs As BindingSource = New BindingSource
dAdapter.Fill(DS, "dsCustomer")
Me.dataGridView1.AutoGenerateColumns = false
Dim dt As DataTable = DS.Tables("dsCustomer")
bs.DataSource = DS.Tables("dsCustomer")
Me.dv = New DataView(DS.Tables("dsCustomer"))
Me.dataGridView1.DataSource = bs
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
'will do the search for CompanyName name everytime you type in @ the textbox search
Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.dv.RowFilter = ("CompanyName like '%' + '" _
+ (textBox1.Text + "' + '%' "))
Me.dataGridView1.DataSource = Me.dv
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
Me.customersTableAdapter.Fill(Me.northwindDataSet.Customers)
End Sub
End Class
End Namespace
另一答案
您可以使用MinFilterLength属性。
MinFilterLength = "2"
另一答案
在这个控件的Ajax版本中,我们有MinFilterLength
添加了这种行为,正如你在这个ASP.NET AutoCompleteBox DEMO中看到的那样。
用于ASP.NET AJAX Q3 2013版本的Telerik®UI介绍了:
MinFilterLength
- 在控件启动对其DataSource的请求之前设置键入文本的最小长度。
以上是关于输入3个字符后如何在autocompletebox中建议?的主要内容,如果未能解决你的问题,请参考以下文章
WPF Toolkit AutoCompleteBox 实体类绑定 关键字自定义关联搜索匹配
Silverlight AutoCompleteBox 样式?