实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
Imports System.Windows.Controls.Primitives Imports System.Windows.Threading <TemplatePart(Name:=AutoCompleteTextBox.PartEditor, Type:=GetType(TextBox))> _ <TemplatePart(Name:=AutoCompleteTextBox.PartPopup, Type:=GetType(Popup))> _ <TemplatePart(Name:=AutoCompleteTextBox.PartSelector, Type:=GetType(Selector))> _ Public Class AutoCompleteTextBox Inherits Control #Region "Fields" Public Const PartEditor As String = "PART_Editor" Public Const PartPopup As String = "PART_Popup" Public Const PartSelector As String = "PART_Selector" Public Shared ReadOnly DelayProperty As DependencyProperty = DependencyProperty.Register("Delay", GetType(Integer), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(200)) Public Shared ReadOnly DisplayMemberProperty As DependencyProperty = DependencyProperty.Register("DisplayMember", GetType(String), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(String.Empty)) Public Shared ReadOnly IsDropDownOpenProperty As DependencyProperty = DependencyProperty.Register("IsDropDownOpen", GetType(Boolean), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(False)) Public Shared ReadOnly IsPopulatingProperty As DependencyProperty = DependencyProperty.Register("IsPopulating", GetType(Boolean), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(False)) Public Shared ReadOnly IsReadOnlyProperty As DependencyProperty = DependencyProperty.Register("IsReadOnly", GetType(Boolean), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(False)) Public Shared ReadOnly ItemTemplateProperty As DependencyProperty = DependencyProperty.Register("ItemTemplate", GetType(DataTemplate), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(Nothing)) Public Shared ReadOnly ProviderProperty As DependencyProperty = DependencyProperty.Register("Provider", GetType(ISuggestionProvider), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(Nothing)) Public Shared ReadOnly SelectedItemProperty As DependencyProperty = DependencyProperty.Register("SelectedItem", GetType(Object), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(Nothing)) Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(String.Empty)) Private _bindingEvaluator As BindingEvaluator Private _editor As TextBox Private _fetchTimer As DispatcherTimer Private _filter As String Private _isUpdatingText As Boolean Private _itemsSelector As Selector Private _popup As Popup Private _selectionAdapter As SelectionAdapter #End Region 'Fields #Region "Constructors" Shared Sub New() DefaultStyleKeyProperty.OverrideMetadata(GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(GetType(AutoCompleteTextBox))) End Sub #End Region 'Constructors #Region "Properties" Public Property BindingEvaluator() As BindingEvaluator Get Return _bindingEvaluator End Get Set(ByVal value As BindingEvaluator) _bindingEvaluator = value End Set End Property Public Property Delay() As Integer Get Return GetValue(DelayProperty) End Get Set(ByVal value As Integer) SetValue(DelayProperty, value) End Set End Property Public Property DisplayMember() As String Get Return GetValue(DisplayMemberProperty) End Get Set(ByVal value As String) SetValue(DisplayMemberProperty, value) End Set End Property Public Property Editor() As TextBox Get Return _editor End Get Set(ByVal value As TextBox) _editor = value End Set End Property Public Property FetchTimer() As DispatcherTimer Get Return _fetchTimer End Get Set(ByVal value As DispatcherTimer) _fetchTimer = value End Set End Property Public Property Filter() As String Get Return _filter End Get Set(ByVal value As String) _filter = value End Set End Property Public Property IsDropDownOpen() As Boolean Get Return GetValue(IsDropDownOpenProperty) End Get Set(ByVal value As Boolean) SetValue(IsDropDownOpenProperty, value) End Set End Property Public Property IsPopulating() As Boolean Get Return GetValue(IsPopulatingProperty) End Get Set(ByVal value As Boolean) SetValue(IsPopulatingProperty, value) End Set End Property Public Property IsReadOnly() As Boolean Get Return GetValue(IsReadOnlyProperty) End Get Set(ByVal value As Boolean) SetValue(IsReadOnlyProperty, value) End Set End Property Public Property ItemsSelector() As Selector Get Return _itemsSelector End Get Set(ByVal value As Selector) _itemsSelector = value End Set End Property Public Property ItemTemplate() As DataTemplate Get Return GetValue(ItemTemplateProperty) End Get Set(ByVal value As DataTemplate) SetValue(ItemTemplateProperty, value) End Set End Property Public Property Popup() As Popup Get Return _popup End Get Set(ByVal value As Popup) _popup = value End Set End Property Public Property Provider() As ISuggestionProvider Get Return GetValue(ProviderProperty) End Get Set(ByVal value As ISuggestionProvider) SetValue(ProviderProperty, value) End Set End Property Public Property SelectedItem() As Object Get Return GetValue(SelectedItemProperty) End Get Set(ByVal value As Object) SetValue(SelectedItemProperty, value) End Set End Property Public Property SelectionAdapter() As SelectionAdapter Get Return _selectionAdapter End Get Set(ByVal value As SelectionAdapter) _selectionAdapter = value End Set End Property Public Property Text() As String Get Return GetValue(TextProperty) End Get Set(ByVal value As String) SetValue(TextProperty, value) End Set End Property #End Region 'Properties #Region "Methods" Public Overrides Sub OnApplyTemplate() MyBase.OnApplyTemplate() Editor = Template.FindName(PartEditor, Me) Popup = Template.FindName(PartPopup, Me) ItemsSelector = Template.FindName(PartSelector, Me) BindingEvaluator = New BindingEvaluator(New Binding(DisplayMember)) If Editor IsNot Nothing Then AddHandler Editor.TextChanged, AddressOf OnEditroTextChanged AddHandler Editor.PreviewKeyDown, AddressOf OnEditorKeyDown AddHandler Editor.LostFocus, AddressOf OnEditorLostFocus End If If Popup IsNot Nothing Then Popup.StaysOpen = False AddHandler Popup.Opened, AddressOf OnPopupOpened AddHandler Popup.Closed, AddressOf OnPopupClosed End If If ItemsSelector IsNot Nothing Then SelectionAdapter = New SelectionAdapter(ItemsSelector) AddHandler SelectionAdapter.Commit, AddressOf OnSelectionAdapterCommit AddHandler SelectionAdapter.Cancel, AddressOf OnSelectionAdapterCancel AddHandler SelectionAdapter.SelectionChanged, AddressOf OnSelectionAdapterSelectionChanged End If End Sub Private Function GetDisplayText(ByVal dataItem As Object) As String If BindingEvaluator Is Nothing Then BindingEvaluator = New BindingEvaluator(New Binding(DisplayMember)) End If If dataItem Is Nothing Then Return String.Empty End If If String.IsNullOrEmpty(DisplayMember) Then Return dataItem.ToString() End If Return BindingEvaluator.Evaluate(dataItem) End Function Private Sub OnEditorKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) If SelectionAdapter IsNot Nothing Then SelectionAdapter.HandleKeyDown(e) End If End Sub Private Sub OnEditorLostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) IsDropDownOpen = False End Sub Private Sub OnEditroTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs) If _isUpdatingText Then Return If FetchTimer Is Nothing Then FetchTimer = New DispatcherTimer FetchTimer.Interval = TimeSpan.FromMilliseconds(Delay) AddHandler FetchTimer.Tick, AddressOf OnFetchTimerTick End If FetchTimer.IsEnabled = False FetchTimer.Stop() If Editor.Text.Length > 0 Then FetchTimer.IsEnabled = True FetchTimer.Start() Else IsDropDownOpen = False End If End Sub Private Sub OnFetchTimerTick(ByVal sender As Object, ByVal e As EventArgs) FetchTimer.IsEnabled = False FetchTimer.Stop() If Provider IsNot Nothing AndAlso ItemsSelector IsNot Nothing Then Filter = Editor.Text ItemsSelector.ItemsSource = Provider.GetSuggestions(Editor.Text) ItemsSelector.SelectedIndex = -1 If ItemsSelector.HasItems AndAlso IsKeyboardFocusWithin Then IsDropDownOpen = True Else IsDropDownOpen = False End If End If End Sub Private Sub OnPopupClosed(ByVal sender As Object, ByVal e As EventArgs) End Sub Private Sub OnPopupOpened(ByVal sender As Object, ByVal e As EventArgs) End Sub Private Sub OnSelectionAdapterCancel() IsDropDownOpen = False End Sub Private Sub OnSelectionAdapterCommit() SelectedItem = ItemsSelector.SelectedItem _isUpdatingText = True Editor.Text = GetDisplayText(ItemsSelector.SelectedItem) _isUpdatingText = False IsDropDownOpen = False End Sub Private Sub OnSelectionAdapterSelectionChanged() _isUpdatingText = True If ItemsSelector.SelectedItem Is Nothing Then Editor.Text = Filter Else Editor.Text = GetDisplayText(ItemsSelector.SelectedItem) End If Editor.SelectionStart = Editor.Text.Length Editor.SelectionLength = 0 _isUpdatingText = False End Sub #End Region 'Methods End Class
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论