|
|
|
Title:
|
Auto Complete TextBox C#
|
Language:
|
C#
|
|
Description:
|
See Auto Complete TextBox XAML. see notes for link
|
Views:
|
182
|
|
Author:
|
Phil Vollhardt
|
Date Added:
|
8/8/2008
|
Copy
|
Code
|
|
1/* Auto Complete TextBox
2 * by Phil Vollhardt
3 * thephilv@gmail.com
4 */
5using System;
6using System.IO;
7using System.Windows;
8using System.Windows.Controls;
9using System.Windows.Data;
10using System.Collections.Generic;
11
12namespace philVsControls {
13 public partial class AutoCompTextBox {
14 protected List<string> _values;
15 protected CollectionViewSource _viewS;
16 public static readonly DependencyProperty HorizontalScrollbarProperty = DependencyProperty.Register("HorizontalScrollbar", typeof(ScrollBarVisibility),
17 typeof(AutoCompTextBox), new UIPropertyMetadata(ScrollBarVisibility.Disabled, new PropertyChangedCallback(OnHorizontalScrollbarChanged),
18 new CoerceValueCallback(OnCoerceHorizontalScrollbar)));
19 public static readonly DependencyProperty VerticalScrollbarProperty = DependencyProperty.Register("VerticalScrollbar", typeof(ScrollBarVisibility),
20 typeof(AutoCompTextBox), new UIPropertyMetadata(ScrollBarVisibility.Disabled, new PropertyChangedCallback(OnVerticalScrollbarChanged),
21 new CoerceValueCallback(OnCoerceVerticalScrollbar)));
22 public static readonly DependencyProperty SourceObjDataProviderProperty = DependencyProperty.Register("SourceObjDataProvider", typeof(ObjectDataProvider),
23 typeof(AutoCompTextBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnSourceObjDataProviderChanged),
24 new CoerceValueCallback(OnCoerceSourceObjDataProvider)));
25 public static readonly DependencyProperty DropBoxMaxHeightProperty = DependencyProperty.Register("DropBoxMaxHeight", typeof(double), typeof(AutoCompTextBox),
26 new UIPropertyMetadata(100.0, new PropertyChangedCallback(OnDropBoxMaxHeightChanged), new CoerceValueCallback(OnCoerceDropBoxMaxHeight)));
27
28 public AutoCompTextBox() {
29 this.InitializeComponent();
30
31 // Insert code required on object creation below this point.
32 _viewS = (CollectionViewSource)this.FindResource("_vs");
33 _tb.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(_tb_PreviewKeyDown);
34 }
35
36 protected void _tb_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
37 switch (e.Key) {
38 case System.Windows.Input.Key.Up:
39 if (_lb.SelectedIndex > 0) {
40 _lb.SelectedIndex -= 1;
41 _lb.ScrollIntoView(_lb.SelectedItem);
42 }
43 break;
44 case System.Windows.Input.Key.Down:
45 if (_lb.SelectedIndex < _lb.Items.Count) {
46 _lb.SelectedIndex += 1;
47 _lb.ScrollIntoView(_lb.SelectedItem);
48 }
49 break;
50 case System.Windows.Input.Key.Enter:
51 if (_lb.Items.Count > 0) {
52 _tb.Text = _lb.SelectedItem.ToString();
53 _lb.Visibility = Visibility.Hidden;
54 _tb.SelectionStart = _tb.Text.Length;
55 }
56 break;
57 }
58 }
59
60
61 protected void CollectionViewSource_Filter(object sender, FilterEventArgs e) {
62 e.Accepted = e.Item.ToString().StartsWith(_tb.Text, StringComparison.CurrentCultureIgnoreCase);
63
64 }
65
66 protected void _tb_TextChanged(object sender, TextChangedEventArgs e) {
67
68 if (null != _viewS.Source) {
69 this._lb.Visibility = this._tb.Text != "" ? Visibility.Visible : Visibility.Hidden;
70 _viewS.View.Refresh();
71 }
72 this.Text = _tb.Text;
73
74 }
75
76 protected void _uc_SizeChanged(object sender, SizeChangedEventArgs e) {
77 this.Resources["_width"] = e.NewSize.Width;
78 }
79 #region horizscrollbar dp ...
80
81 private static object OnCoerceHorizontalScrollbar(DependencyObject o, object value) {
82 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
83 if (autoCompTextBox != null)
84 return autoCompTextBox.OnCoerceHorizontalScrollbar((ScrollBarVisibility)value);
85 else
86 return value;
87 }
88
89 private static void OnHorizontalScrollbarChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
90 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
91 if (autoCompTextBox != null)
92 autoCompTextBox.OnHorizontalScrollbarChanged((ScrollBarVisibility)e.OldValue, (ScrollBarVisibility)e.NewValue);
93 }
94
95 protected virtual ScrollBarVisibility OnCoerceHorizontalScrollbar(ScrollBarVisibility value) {
96 // TODO: Keep the proposed value within the desired range.
97 return value;
98 }
99
100 protected virtual void OnHorizontalScrollbarChanged(ScrollBarVisibility oldValue, ScrollBarVisibility newValue) {
101 // TODO: Add your property changed side-effects. Descendants can override as well.
102 _lb.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, newValue);
103 }
104
105 /// <summary>
106 /// Gets or sets the use of a horizontal scrollbar in dropbox. Default is disabled
107 /// </summary>
108 public ScrollBarVisibility HorizontalScrollbar {
109 // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
110 get {
111 return (ScrollBarVisibility)GetValue(HorizontalScrollbarProperty);
112 }
113 set {
114 SetValue(HorizontalScrollbarProperty, value);
115 }
116 }
117 #endregion
118 #region vertscrollbar dp ...
119
120 private static object OnCoerceVerticalScrollbar(DependencyObject o, object value) {
121 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
122 if (autoCompTextBox != null)
123 return autoCompTextBox.OnCoerceVerticalScrollbar((ScrollBarVisibility)value);
124 else
125 return value;
126 }
127
128 private static void OnVerticalScrollbarChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
129 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
130 if (autoCompTextBox != null)
131 autoCompTextBox.OnVerticalScrollbarChanged((ScrollBarVisibility)e.OldValue, (ScrollBarVisibility)e.NewValue);
132 }
133
134 protected virtual ScrollBarVisibility OnCoerceVerticalScrollbar(ScrollBarVisibility value) {
135 // TODO: Keep the proposed value within the desired range.
136 return value;
137 }
138
139 protected virtual void OnVerticalScrollbarChanged(ScrollBarVisibility oldValue, ScrollBarVisibility newValue) {
140 // TODO: Add your property changed side-effects. Descendants can override as well.
141 _lb.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, newValue);
142 }
143
144 /// <summary>
145 /// Gets or sets the use of a vertical scrollbar in dropbox. Default is disabled
146 /// </summary>
147 public ScrollBarVisibility VerticalScrollbar {
148 // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
149 get {
150 return (ScrollBarVisibility)GetValue(VerticalScrollbarProperty);
151 }
152 set {
153 SetValue(VerticalScrollbarProperty, value);
154 }
155 }
156 #endregion
157 #region SourceObjectDataProvider ...
158
159 private static object OnCoerceSourceObjDataProvider(DependencyObject o, object value) {
160 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
161 if (autoCompTextBox != null)
162 return autoCompTextBox.OnCoerceSourceObjDataProvider((ObjectDataProvider)value);
163 else
164 return value;
165 }
166
167 private static void OnSourceObjDataProviderChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
168 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
169 if (autoCompTextBox != null)
170 autoCompTextBox.OnSourceObjDataProviderChanged((ObjectDataProvider)e.OldValue, (ObjectDataProvider)e.NewValue);
171 }
172
173 protected virtual ObjectDataProvider OnCoerceSourceObjDataProvider(ObjectDataProvider value) {
174 // TODO: Keep the proposed value within the desired range.
175 return value;
176 }
177
178 protected virtual void OnSourceObjDataProviderChanged(ObjectDataProvider oldValue, ObjectDataProvider newValue) {
179 // TODO: Add your property changed side-effects. Descendants can override as well.
180 _viewS.SetValue(CollectionViewSource.SourceProperty, newValue);
181 }
182
183 public ObjectDataProvider SourceObjDataProvider {
184 // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
185 get {
186 return (ObjectDataProvider)GetValue(SourceObjDataProviderProperty);
187 }
188 set {
189 SetValue(SourceObjDataProviderProperty, value);
190 }
191 }
192 #endregion
193 #region Max Height of listbox ...
194
195 private static object OnCoerceDropBoxMaxHeight(DependencyObject o, object value) {
196 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
197 if (autoCompTextBox != null)
198 return autoCompTextBox.OnCoerceDropBoxMaxHeight((double)value);
199 else
200 return value;
201 }
202
203 private static void OnDropBoxMaxHeightChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
204 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
205 if (autoCompTextBox != null)
206 autoCompTextBox.OnDropBoxMaxHeightChanged((double)e.OldValue, (double)e.NewValue);
207 }
208
209 protected virtual double OnCoerceDropBoxMaxHeight(double value) {
210 if (value < 0) value = 0;
211 return value;
212 }
213
214 protected virtual void OnDropBoxMaxHeightChanged(double oldValue, double newValue) {
215 // TODO: Add your property changed side-effects. Descendants can override as well.
216 _lb.SetValue(ListBox.MaxHeightProperty, newValue);
217 }
218
219 /// <summary>
220 /// Gets or sets the MaxHeight of the listbox. dependency property. default is 100
221 /// </summary>
222 public double DropBoxMaxHeight {
223 // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
224 get {
225 return (double)GetValue(DropBoxMaxHeightProperty);
226 }
227 set {
228 SetValue(DropBoxMaxHeightProperty, value);
229 }
230 }
231 #endregion
232 #region Text dp ...
233
234 public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(AutoCompTextBox), new UIPropertyMetadata("", new PropertyChangedCallback(OnTextChanged), new CoerceValueCallback(OnCoerceText)));
235
236 private static object OnCoerceText(DependencyObject o, object value) {
237 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
238 if (autoCompTextBox != null)
239 return autoCompTextBox.OnCoerceText((string)value);
240 else
241 return value;
242 }
243
244 private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) {
245 AutoCompTextBox autoCompTextBox = o as AutoCompTextBox;
246 if (autoCompTextBox != null)
247 autoCompTextBox.OnTextChanged((string)e.OldValue, (string)e.NewValue);
248 }
249
250 protected virtual string OnCoerceText(string value) {
251 // TODO: Keep the proposed value within the desired range.
252 return value;
253 }
254
255 protected virtual void OnTextChanged(string oldValue, string newValue) {
256 // TODO: Add your property changed side-effects. Descendants can override as well.
257 _tb.Text = newValue;
258 }
259
260 /// <summary>
261 /// Gets or Sets the text in the textbox. dependency property.
262 /// </summary>
263 public string Text {
264 // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
265 get {
266 return (string)GetValue(TextProperty);
267 }
268 set {
269 SetValue(TextProperty, value);
270 }
271 }
272 #endregion
273
274 }
275}
|
|
Usage
|
See Auto Complete TextBox XAML. see notes for link
|
|
Notes
|
|
http://www.codekeep.net/snippets/403cd3d6-6139-43c1-a3b4-f6dcacfbfc7a.aspx
|
|
|