forked from golfandsurf/Campah
/
AutoCompleteTextBox.xaml.cs
186 lines (166 loc) · 6.05 KB
/
AutoCompleteTextBox.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFAutoCompleteTextbox
{
/// <summary>
/// Interaction logic for AutoCompleteTextBox.xaml
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
public partial class AutoCompleteTextBox : Canvas
{
#region Members
private VisualCollection controls;
private TextBox textBox;
public ComboBox comboBox;
private ObservableCollection<AutoCompleteEntry> autoCompletionList;
private System.Timers.Timer keypressTimer;
private delegate void TextChangedCallback();
private bool insertText;
private int delayTime;
private int searchThreshold;
#endregion
#region Constructor
public AutoCompleteTextBox()
{
controls = new VisualCollection(this);
InitializeComponent();
autoCompletionList = new ObservableCollection<AutoCompleteEntry>();
searchThreshold = 2; // default threshold to 2 char
// set up the key press timer
keypressTimer = new System.Timers.Timer();
keypressTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
// set up the text box and the combo box
comboBox = new ComboBox();
comboBox.IsSynchronizedWithCurrentItem = true;
comboBox.IsTabStop = false;
comboBox.SelectionChanged += new SelectionChangedEventHandler(comboBox_SelectionChanged);
textBox = new TextBox();
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
textBox.VerticalContentAlignment = VerticalAlignment.Center;
controls.Add(comboBox);
controls.Add(textBox);
}
#endregion
#region Methods
public string Text
{
get
{
return textBox.Text;
}
set
{
insertText = true;
textBox.Text = value;
CampahApp.RunningData.Instance.CurrentItemText = value;
}
}
public int DelayTime
{
get { return delayTime; }
set { delayTime = value; }
}
public int Threshold
{
get { return searchThreshold; }
set { searchThreshold = value; }
}
public void AddItem(AutoCompleteEntry entry)
{
autoCompletionList.Add(entry);
}
public void ClearList()
{
autoCompletionList.Clear();
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (null != comboBox.SelectedItem)
{
insertText = true;
ComboBoxItem cbItem = (ComboBoxItem)comboBox.SelectedItem;
textBox.Text = cbItem.Content.ToString();
}
}
private void TextChanged()
{
try
{
comboBox.Items.Clear();
if (textBox.Text.Length >= searchThreshold)
{
foreach (AutoCompleteEntry entry in autoCompletionList)
{
foreach (string word in entry.KeywordStrings)
{
if (word.StartsWith(textBox.Text, StringComparison.CurrentCultureIgnoreCase))
{
ComboBoxItem cbItem = new ComboBoxItem();
cbItem.Content = entry.ToString();
comboBox.Items.Add(cbItem);
break;
}
}
}
comboBox.IsDropDownOpen = comboBox.HasItems;
}
else
{
comboBox.IsDropDownOpen = false;
}
}
catch { }
}
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
keypressTimer.Stop();
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new TextChangedCallback(this.TextChanged));
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
CampahApp.RunningData.Instance.CurrentItemText = textBox.Text;
// text was not typed, do nothing and consume the flag
if (insertText == true) insertText = false;
// if the delay time is set, delay handling of text changed
else
{
if (delayTime > 0)
{
keypressTimer.Interval = delayTime;
keypressTimer.Start();
}
else TextChanged();
}
CampahApp.CurrentSelection.Name = textBox.Text;
}
protected override Size ArrangeOverride(Size arrangeSize)
{
textBox.Arrange(new Rect(arrangeSize));
comboBox.Arrange(new Rect(arrangeSize));
return base.ArrangeOverride(arrangeSize);
}
protected override Visual GetVisualChild(int index)
{
return controls[index];
}
protected override int VisualChildrenCount
{
get { return controls.Count; }
}
#endregion
}
}