PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/toolkit/Microsoft.Phone.Controls.Toolkit/AutoCompleteBox/AutoCompleteBox.cs

https://bitbucket.org/jeremejevs/word-steps
C# | 2678 lines | 1397 code | 254 blank | 1027 comment | 300 complexity | 377f309c4951a40948130697eea689a5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Collections.Specialized;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.Linq;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Data;
  16. using System.Windows.Input;
  17. using System.Windows.Markup;
  18. using System.Windows.Media;
  19. using System.Windows.Threading;
  20. #if WINDOWS_PHONE
  21. namespace Microsoft.Phone.Controls
  22. #else
  23. namespace System.Windows.Controls
  24. #endif
  25. {
  26. /// <summary>
  27. /// Represents a control that provides a text box for user input and a
  28. /// drop-down that contains possible matches based on the input in the text
  29. /// box.
  30. /// </summary>
  31. /// <QualityBand>Stable</QualityBand>
  32. [TemplatePart(Name = AutoCompleteBox.ElementSelectionAdapter, Type = typeof(ISelectionAdapter))]
  33. [TemplatePart(Name = AutoCompleteBox.ElementSelector, Type = typeof(Selector))]
  34. [TemplatePart(Name = AutoCompleteBox.ElementTextBox, Type = typeof(TextBox))]
  35. [TemplatePart(Name = AutoCompleteBox.ElementPopup, Type = typeof(Popup))]
  36. [StyleTypedProperty(Property = AutoCompleteBox.ElementTextBoxStyle, StyleTargetType = typeof(TextBox))]
  37. [StyleTypedProperty(Property = AutoCompleteBox.ElementItemContainerStyle, StyleTargetType = typeof(ListBox))]
  38. [TemplateVisualState(Name = VisualStates.StateNormal, GroupName = VisualStates.GroupCommon)]
  39. [TemplateVisualState(Name = VisualStates.StateMouseOver, GroupName = VisualStates.GroupCommon)]
  40. [TemplateVisualState(Name = VisualStates.StatePressed, GroupName = VisualStates.GroupCommon)]
  41. [TemplateVisualState(Name = VisualStates.StateDisabled, GroupName = VisualStates.GroupCommon)]
  42. [TemplateVisualState(Name = VisualStates.StateFocused, GroupName = VisualStates.GroupFocus)]
  43. [TemplateVisualState(Name = VisualStates.StateUnfocused, GroupName = VisualStates.GroupFocus)]
  44. [TemplateVisualState(Name = VisualStates.StatePopupClosed, GroupName = VisualStates.GroupPopup)]
  45. [TemplateVisualState(Name = VisualStates.StatePopupOpened, GroupName = VisualStates.GroupPopup)]
  46. [TemplateVisualState(Name = VisualStates.StateValid, GroupName = VisualStates.GroupValidation)]
  47. [TemplateVisualState(Name = VisualStates.StateInvalidFocused, GroupName = VisualStates.GroupValidation)]
  48. [TemplateVisualState(Name = VisualStates.StateInvalidUnfocused, GroupName = VisualStates.GroupValidation)]
  49. [SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Large implementation keeps the components contained.")]
  50. [ContentProperty("ItemsSource")]
  51. public partial class AutoCompleteBox : Control, IUpdateVisualState
  52. {
  53. #region Template part and style names
  54. /// <summary>
  55. /// Specifies the name of the selection adapter TemplatePart.
  56. /// </summary>
  57. private const string ElementSelectionAdapter = "SelectionAdapter";
  58. /// <summary>
  59. /// Specifies the name of the Selector TemplatePart.
  60. /// </summary>
  61. private const string ElementSelector = "Selector";
  62. /// <summary>
  63. /// Specifies the name of the Popup TemplatePart.
  64. /// </summary>
  65. private const string ElementPopup = "Popup";
  66. /// <summary>
  67. /// The name for the text box part.
  68. /// </summary>
  69. private const string ElementTextBox = "Text";
  70. /// <summary>
  71. /// The name for the text box style.
  72. /// </summary>
  73. private const string ElementTextBoxStyle = "TextBoxStyle";
  74. /// <summary>
  75. /// The name for the adapter's item container style.
  76. /// </summary>
  77. private const string ElementItemContainerStyle = "ItemContainerStyle";
  78. #endregion
  79. /// <summary>
  80. /// Gets or sets a local cached copy of the items data.
  81. /// </summary>
  82. private List<object> _items;
  83. /// <summary>
  84. /// Gets or sets the observable collection that contains references to
  85. /// all of the items in the generated view of data that is provided to
  86. /// the selection-style control adapter.
  87. /// </summary>
  88. private ObservableCollection<object> _view;
  89. /// <summary>
  90. /// Gets or sets a value to ignore a number of pending change handlers.
  91. /// The value is decremented after each use. This is used to reset the
  92. /// value of properties without performing any of the actions in their
  93. /// change handlers.
  94. /// </summary>
  95. /// <remarks>The int is important as a value because the TextBox
  96. /// TextChanged event does not immediately fire, and this will allow for
  97. /// nested property changes to be ignored.</remarks>
  98. private int _ignoreTextPropertyChange;
  99. /// <summary>
  100. /// Gets or sets a value indicating whether to ignore calling a pending
  101. /// change handlers.
  102. /// </summary>
  103. private bool _ignorePropertyChange;
  104. /// <summary>
  105. /// Gets or sets a value indicating whether to ignore the selection
  106. /// changed event.
  107. /// </summary>
  108. private bool _ignoreTextSelectionChange;
  109. /// <summary>
  110. /// Gets or sets a value indicating whether to skip the text update
  111. /// processing when the selected item is updated.
  112. /// </summary>
  113. private bool _skipSelectedItemTextUpdate;
  114. /// <summary>
  115. /// Gets or sets the last observed text box selection start location.
  116. /// </summary>
  117. private int _textSelectionStart;
  118. /// <summary>
  119. /// Gets or sets a value indicating whether the user is in the process
  120. /// of inputting text. This is used so that we do not update
  121. /// _textSelectionStart while the user is using an IME.
  122. /// </summary>
  123. private bool _inputtingText;
  124. /// <summary>
  125. /// Gets or sets a value indicating whether the user initiated the
  126. /// current populate call.
  127. /// </summary>
  128. private bool _userCalledPopulate;
  129. /// <summary>
  130. /// A value indicating whether the popup has been opened at least once.
  131. /// </summary>
  132. private bool _popupHasOpened;
  133. /// <summary>
  134. /// Gets or sets the DispatcherTimer used for the MinimumPopulateDelay
  135. /// condition for auto completion.
  136. /// </summary>
  137. private DispatcherTimer _delayTimer;
  138. /// <summary>
  139. /// Gets or sets a value indicating whether a read-only dependency
  140. /// property change handler should allow the value to be set. This is
  141. /// used to ensure that read-only properties cannot be changed via
  142. /// SetValue, etc.
  143. /// </summary>
  144. private bool _allowWrite;
  145. /// <summary>
  146. /// Gets or sets the helper that provides all of the standard
  147. /// interaction functionality. Making it internal for subclass access.
  148. /// </summary>
  149. internal InteractionHelper Interaction { get; set; }
  150. /// <summary>
  151. /// Gets or sets the BindingEvaluator, a framework element that can
  152. /// provide updated string values from a single binding.
  153. /// </summary>
  154. private BindingEvaluator<string> _valueBindingEvaluator;
  155. /// <summary>
  156. /// A weak event listener for the collection changed event.
  157. /// </summary>
  158. private WeakEventListener<AutoCompleteBox, object, NotifyCollectionChangedEventArgs> _collectionChangedWeakEventListener;
  159. #region public int MinimumPrefixLength
  160. /// <summary>
  161. /// Gets or sets the minimum number of characters required to be entered
  162. /// in the text box before the
  163. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> displays
  164. /// possible matches.
  165. /// matches.
  166. /// </summary>
  167. /// <value>
  168. /// The minimum number of characters to be entered in the text box
  169. /// before the <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" />
  170. /// displays possible matches. The default is 1.
  171. /// </value>
  172. /// <remarks>
  173. /// If you set MinimumPrefixLength to -1, the AutoCompleteBox will
  174. /// not provide possible matches. There is no maximum value, but
  175. /// setting MinimumPrefixLength to value that is too large will
  176. /// prevent the AutoCompleteBox from providing possible matches as well.
  177. /// </remarks>
  178. public int MinimumPrefixLength
  179. {
  180. get { return (int)GetValue(MinimumPrefixLengthProperty); }
  181. set { SetValue(MinimumPrefixLengthProperty, value); }
  182. }
  183. /// <summary>
  184. /// Identifies the
  185. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.MinimumPrefixLength" />
  186. /// dependency property.
  187. /// </summary>
  188. /// <value>The identifier for the
  189. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.MinimumPrefixLength" />
  190. /// dependency property.</value>
  191. public static readonly DependencyProperty MinimumPrefixLengthProperty =
  192. DependencyProperty.Register(
  193. "MinimumPrefixLength",
  194. typeof(int),
  195. typeof(AutoCompleteBox),
  196. new PropertyMetadata(1, OnMinimumPrefixLengthPropertyChanged));
  197. /// <summary>
  198. /// MinimumPrefixLengthProperty property changed handler.
  199. /// </summary>
  200. /// <param name="d">AutoCompleteBox that changed its MinimumPrefixLength.</param>
  201. /// <param name="e">Event arguments.</param>
  202. [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "MinimumPrefixLength is the name of the actual dependency property.")]
  203. private static void OnMinimumPrefixLengthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  204. {
  205. int newValue = (int)e.NewValue;
  206. if (newValue < 0 && newValue != -1)
  207. {
  208. throw new ArgumentOutOfRangeException("MinimumPrefixLength");
  209. }
  210. }
  211. #endregion public int MinimumPrefixLength
  212. #region public int MinimumPopulateDelay
  213. /// <summary>
  214. /// Gets or sets the minimum delay, in milliseconds, after text is typed
  215. /// in the text box before the
  216. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control
  217. /// populates the list of possible matches in the drop-down.
  218. /// </summary>
  219. /// <value>The minimum delay, in milliseconds, after text is typed in
  220. /// the text box, but before the
  221. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> populates
  222. /// the list of possible matches in the drop-down. The default is 0.</value>
  223. /// <exception cref="T:System.ArgumentException">The set value is less than 0.</exception>
  224. public int MinimumPopulateDelay
  225. {
  226. get { return (int)GetValue(MinimumPopulateDelayProperty); }
  227. set { SetValue(MinimumPopulateDelayProperty, value); }
  228. }
  229. /// <summary>
  230. /// Identifies the
  231. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.MinimumPopulateDelay" />
  232. /// dependency property.
  233. /// </summary>
  234. /// <value>The identifier for the
  235. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.MinimumPopulateDelay" />
  236. /// dependency property.</value>
  237. public static readonly DependencyProperty MinimumPopulateDelayProperty =
  238. DependencyProperty.Register(
  239. "MinimumPopulateDelay",
  240. typeof(int),
  241. typeof(AutoCompleteBox),
  242. new PropertyMetadata(OnMinimumPopulateDelayPropertyChanged));
  243. /// <summary>
  244. /// MinimumPopulateDelayProperty property changed handler. Any current
  245. /// dispatcher timer will be stopped. The timer will not be restarted
  246. /// until the next TextUpdate call by the user.
  247. /// </summary>
  248. /// <param name="d">AutoCompleteTextBox that changed its
  249. /// MinimumPopulateDelay.</param>
  250. /// <param name="e">Event arguments.</param>
  251. [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "The exception is most likely to be called through the CLR property setter.")]
  252. private static void OnMinimumPopulateDelayPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  253. {
  254. AutoCompleteBox source = d as AutoCompleteBox;
  255. if (source._ignorePropertyChange)
  256. {
  257. source._ignorePropertyChange = false;
  258. return;
  259. }
  260. int newValue = (int)e.NewValue;
  261. if (newValue < 0)
  262. {
  263. source._ignorePropertyChange = true;
  264. d.SetValue(e.Property, e.OldValue);
  265. //throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.AutoComplete_OnMinimumPopulateDelayPropertyChanged_InvalidValue, newValue), "value");
  266. }
  267. // Stop any existing timer
  268. if (source._delayTimer != null)
  269. {
  270. source._delayTimer.Stop();
  271. if (newValue == 0)
  272. {
  273. source._delayTimer = null;
  274. }
  275. }
  276. // Create or clear a dispatcher timer instance
  277. if (newValue > 0 && source._delayTimer == null)
  278. {
  279. source._delayTimer = new DispatcherTimer();
  280. source._delayTimer.Tick += source.PopulateDropDown;
  281. }
  282. // Set the new tick interval
  283. if (newValue > 0 && source._delayTimer != null)
  284. {
  285. source._delayTimer.Interval = TimeSpan.FromMilliseconds(newValue);
  286. }
  287. }
  288. #endregion public int MinimumPopulateDelay
  289. #region public bool IsTextCompletionEnabled
  290. /// <summary>
  291. /// Gets or sets a value indicating whether the first possible match
  292. /// found during the filtering process will be displayed automatically
  293. /// in the text box.
  294. /// </summary>
  295. /// <value>
  296. /// True if the first possible match found will be displayed
  297. /// automatically in the text box; otherwise, false. The default is
  298. /// false.
  299. /// </value>
  300. public bool IsTextCompletionEnabled
  301. {
  302. get { return (bool)GetValue(IsTextCompletionEnabledProperty); }
  303. set { SetValue(IsTextCompletionEnabledProperty, value); }
  304. }
  305. /// <summary>
  306. /// Identifies the
  307. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.IsTextCompletionEnabled" />
  308. /// dependency property.
  309. /// </summary>
  310. /// <value>The identifier for the
  311. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.IsTextCompletionEnabled" />
  312. /// dependency property.</value>
  313. public static readonly DependencyProperty IsTextCompletionEnabledProperty =
  314. DependencyProperty.Register(
  315. "IsTextCompletionEnabled",
  316. typeof(bool),
  317. typeof(AutoCompleteBox),
  318. new PropertyMetadata(false, null));
  319. #endregion public bool IsTextCompletionEnabled
  320. #region public DataTemplate ItemTemplate
  321. /// <summary>
  322. /// Gets or sets the <see cref="T:System.Windows.DataTemplate" /> used
  323. /// to display each item in the drop-down portion of the control.
  324. /// </summary>
  325. /// <value>The <see cref="T:System.Windows.DataTemplate" /> used to
  326. /// display each item in the drop-down. The default is null.</value>
  327. /// <remarks>
  328. /// You use the ItemTemplate property to specify the visualization
  329. /// of the data objects in the drop-down portion of the AutoCompleteBox
  330. /// control. If your AutoCompleteBox is bound to a collection and you
  331. /// do not provide specific display instructions by using a
  332. /// DataTemplate, the resulting UI of each item is a string
  333. /// representation of each object in the underlying collection.
  334. /// </remarks>
  335. public DataTemplate ItemTemplate
  336. {
  337. get { return GetValue(ItemTemplateProperty) as DataTemplate; }
  338. set { SetValue(ItemTemplateProperty, value); }
  339. }
  340. /// <summary>
  341. /// Identifies the
  342. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemTemplate" />
  343. /// dependency property.
  344. /// </summary>
  345. /// <value>The identifier for the
  346. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemTemplate" />
  347. /// dependency property.</value>
  348. public static readonly DependencyProperty ItemTemplateProperty =
  349. DependencyProperty.Register(
  350. "ItemTemplate",
  351. typeof(DataTemplate),
  352. typeof(AutoCompleteBox),
  353. new PropertyMetadata(null));
  354. #endregion public DataTemplate ItemTemplate
  355. #region public Style ItemContainerStyle
  356. /// <summary>
  357. /// Gets or sets the <see cref="T:System.Windows.Style" /> that is
  358. /// applied to the selection adapter contained in the drop-down portion
  359. /// of the <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" />
  360. /// control.
  361. /// </summary>
  362. /// <value>The <see cref="T:System.Windows.Style" /> applied to the
  363. /// selection adapter contained in the drop-down portion of the
  364. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  365. /// The default is null.</value>
  366. /// <remarks>
  367. /// The default selection adapter contained in the drop-down is a
  368. /// ListBox control.
  369. /// </remarks>
  370. public Style ItemContainerStyle
  371. {
  372. get { return GetValue(ItemContainerStyleProperty) as Style; }
  373. set { SetValue(ItemContainerStyleProperty, value); }
  374. }
  375. /// <summary>
  376. /// Identifies the
  377. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemContainerStyle" />
  378. /// dependency property.
  379. /// </summary>
  380. /// <value>The identifier for the
  381. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemContainerStyle" />
  382. /// dependency property.</value>
  383. public static readonly DependencyProperty ItemContainerStyleProperty =
  384. DependencyProperty.Register(
  385. ElementItemContainerStyle,
  386. typeof(Style),
  387. typeof(AutoCompleteBox),
  388. new PropertyMetadata(null, null));
  389. #endregion public Style ItemContainerStyle
  390. #region public Style TextBoxStyle
  391. /// <summary>
  392. /// Gets or sets the <see cref="T:System.Windows.Style" /> applied to
  393. /// the text box portion of the
  394. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  395. /// </summary>
  396. /// <value>The <see cref="T:System.Windows.Style" /> applied to the text
  397. /// box portion of the
  398. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  399. /// The default is null.</value>
  400. public Style TextBoxStyle
  401. {
  402. get { return GetValue(TextBoxStyleProperty) as Style; }
  403. set { SetValue(TextBoxStyleProperty, value); }
  404. }
  405. /// <summary>
  406. /// Identifies the
  407. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.TextBoxStyle" />
  408. /// dependency property.
  409. /// </summary>
  410. /// <value>The identifier for the
  411. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.TextBoxStyle" />
  412. /// dependency property.</value>
  413. public static readonly DependencyProperty TextBoxStyleProperty =
  414. DependencyProperty.Register(
  415. ElementTextBoxStyle,
  416. typeof(Style),
  417. typeof(AutoCompleteBox),
  418. new PropertyMetadata(null));
  419. #endregion public Style TextBoxStyle
  420. #region public double MaxDropDownHeight
  421. /// <summary>
  422. /// Gets or sets the maximum height of the drop-down portion of the
  423. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  424. /// </summary>
  425. /// <value>The maximum height of the drop-down portion of the
  426. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  427. /// The default is <see cref="F:System.Double.PositiveInfinity" />.</value>
  428. /// <exception cref="T:System.ArgumentException">The specified value is less than 0.</exception>
  429. public double MaxDropDownHeight
  430. {
  431. get { return (double)GetValue(MaxDropDownHeightProperty); }
  432. set { SetValue(MaxDropDownHeightProperty, value); }
  433. }
  434. /// <summary>
  435. /// Identifies the
  436. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.MaxDropDownHeight" />
  437. /// dependency property.
  438. /// </summary>
  439. /// <value>The identifier for the
  440. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.MaxDropDownHeight" />
  441. /// dependency property.</value>
  442. public static readonly DependencyProperty MaxDropDownHeightProperty =
  443. DependencyProperty.Register(
  444. "MaxDropDownHeight",
  445. typeof(double),
  446. typeof(AutoCompleteBox),
  447. new PropertyMetadata(double.PositiveInfinity, OnMaxDropDownHeightPropertyChanged));
  448. /// <summary>
  449. /// MaxDropDownHeightProperty property changed handler.
  450. /// </summary>
  451. /// <param name="d">AutoCompleteTextBox that changed its MaxDropDownHeight.</param>
  452. /// <param name="e">Event arguments.</param>
  453. [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "The exception will be called through a CLR setter in most cases.")]
  454. private static void OnMaxDropDownHeightPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  455. {
  456. AutoCompleteBox source = d as AutoCompleteBox;
  457. if (source._ignorePropertyChange)
  458. {
  459. source._ignorePropertyChange = false;
  460. return;
  461. }
  462. double newValue = (double)e.NewValue;
  463. // Revert to the old value if invalid (negative)
  464. if (newValue < 0)
  465. {
  466. source._ignorePropertyChange = true;
  467. source.SetValue(e.Property, e.OldValue);
  468. //throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.AutoComplete_OnMaxDropDownHeightPropertyChanged_InvalidValue, e.NewValue), "value");
  469. }
  470. source.OnMaxDropDownHeightChanged(newValue);
  471. }
  472. #endregion public double MaxDropDownHeight
  473. #region public bool IsDropDownOpen
  474. /// <summary>
  475. /// Gets or sets a value indicating whether the drop-down portion of
  476. /// the control is open.
  477. /// </summary>
  478. /// <value>
  479. /// True if the drop-down is open; otherwise, false. The default is
  480. /// false.
  481. /// </value>
  482. public bool IsDropDownOpen
  483. {
  484. get { return (bool)GetValue(IsDropDownOpenProperty); }
  485. set { SetValue(IsDropDownOpenProperty, value); }
  486. }
  487. /// <summary>
  488. /// Identifies the
  489. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.IsDropDownOpen" />
  490. /// dependency property.
  491. /// </summary>
  492. /// <value>The identifier for the
  493. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.IsDropDownOpen" />
  494. /// dependency property.</value>
  495. public static readonly DependencyProperty IsDropDownOpenProperty =
  496. DependencyProperty.Register(
  497. "IsDropDownOpen",
  498. typeof(bool),
  499. typeof(AutoCompleteBox),
  500. new PropertyMetadata(false, OnIsDropDownOpenPropertyChanged));
  501. /// <summary>
  502. /// IsDropDownOpenProperty property changed handler.
  503. /// </summary>
  504. /// <param name="d">AutoCompleteTextBox that changed its IsDropDownOpen.</param>
  505. /// <param name="e">Event arguments.</param>
  506. private static void OnIsDropDownOpenPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  507. {
  508. AutoCompleteBox source = d as AutoCompleteBox;
  509. // Ignore the change if requested
  510. if (source._ignorePropertyChange)
  511. {
  512. source._ignorePropertyChange = false;
  513. return;
  514. }
  515. bool oldValue = (bool)e.OldValue;
  516. bool newValue = (bool)e.NewValue;
  517. if (newValue)
  518. {
  519. source.TextUpdated(source.Text, true);
  520. }
  521. else
  522. {
  523. source.ClosingDropDown(oldValue);
  524. }
  525. source.UpdateVisualState(true);
  526. }
  527. #endregion public bool IsDropDownOpen
  528. #region public IEnumerable ItemsSource
  529. /// <summary>
  530. /// Gets or sets a collection that is used to generate the items for the
  531. /// drop-down portion of the
  532. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  533. /// </summary>
  534. /// <value>The collection that is used to generate the items of the
  535. /// drop-down portion of the
  536. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.</value>
  537. public IEnumerable ItemsSource
  538. {
  539. get { return GetValue(ItemsSourceProperty) as IEnumerable; }
  540. set { SetValue(ItemsSourceProperty, value); }
  541. }
  542. /// <summary>
  543. /// Identifies the
  544. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  545. /// dependency property.
  546. /// </summary>
  547. /// <value>The identifier for the
  548. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  549. /// dependency property.</value>
  550. public static readonly DependencyProperty ItemsSourceProperty =
  551. DependencyProperty.Register(
  552. "ItemsSource",
  553. typeof(IEnumerable),
  554. typeof(AutoCompleteBox),
  555. new PropertyMetadata(OnItemsSourcePropertyChanged));
  556. /// <summary>
  557. /// ItemsSourceProperty property changed handler.
  558. /// </summary>
  559. /// <param name="d">AutoCompleteBox that changed its ItemsSource.</param>
  560. /// <param name="e">Event arguments.</param>
  561. private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  562. {
  563. AutoCompleteBox autoComplete = d as AutoCompleteBox;
  564. autoComplete.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
  565. }
  566. #endregion public IEnumerable ItemsSource
  567. #region public object SelectedItem
  568. /// <summary>
  569. /// Gets or sets the selected item in the drop-down.
  570. /// </summary>
  571. /// <value>The selected item in the drop-down.</value>
  572. /// <remarks>
  573. /// If the IsTextCompletionEnabled property is true and text typed by
  574. /// the user matches an item in the ItemsSource collection, which is
  575. /// then displayed in the text box, the SelectedItem property will be
  576. /// a null reference.
  577. /// </remarks>
  578. public object SelectedItem
  579. {
  580. get { return GetValue(SelectedItemProperty) as object; }
  581. set { SetValue(SelectedItemProperty, value); }
  582. }
  583. /// <summary>
  584. /// Identifies the
  585. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.SelectedItem" />
  586. /// dependency property.
  587. /// </summary>
  588. /// <value>The identifier the
  589. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.SelectedItem" />
  590. /// dependency property.</value>
  591. public static readonly DependencyProperty SelectedItemProperty =
  592. DependencyProperty.Register(
  593. "SelectedItem",
  594. typeof(object),
  595. typeof(AutoCompleteBox),
  596. new PropertyMetadata(OnSelectedItemPropertyChanged));
  597. /// <summary>
  598. /// SelectedItemProperty property changed handler. Fires the
  599. /// SelectionChanged event. The event data will contain any non-null
  600. /// removed items and non-null additions.
  601. /// </summary>
  602. /// <param name="d">AutoCompleteBox that changed its SelectedItem.</param>
  603. /// <param name="e">Event arguments.</param>
  604. private static void OnSelectedItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  605. {
  606. AutoCompleteBox source = d as AutoCompleteBox;
  607. if (source._ignorePropertyChange)
  608. {
  609. source._ignorePropertyChange = false;
  610. return;
  611. }
  612. // Update the text display
  613. if (source._skipSelectedItemTextUpdate)
  614. {
  615. source._skipSelectedItemTextUpdate = false;
  616. }
  617. else
  618. {
  619. source.OnSelectedItemChanged(e.NewValue);
  620. }
  621. // Fire the SelectionChanged event
  622. List<object> removed = new List<object>();
  623. if (e.OldValue != null)
  624. {
  625. removed.Add(e.OldValue);
  626. }
  627. List<object> added = new List<object>();
  628. if (e.NewValue != null)
  629. {
  630. added.Add(e.NewValue);
  631. }
  632. source.OnSelectionChanged(new SelectionChangedEventArgs(
  633. #if !SILVERLIGHT
  634. SelectionChangedEvent,
  635. #endif
  636. removed,
  637. added));
  638. }
  639. #if !SILVERLIGHT
  640. /// <summary>
  641. /// Declares the routed event for SelectionChanged.
  642. /// </summary>
  643. public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent(
  644. "SelectionChanged", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(AutoCompleteBox));
  645. #endif
  646. /// <summary>
  647. /// Called when the selected item is changed, updates the text value
  648. /// that is displayed in the text box part.
  649. /// </summary>
  650. /// <param name="newItem">The new item.</param>
  651. private void OnSelectedItemChanged(object newItem)
  652. {
  653. string text;
  654. if (newItem == null)
  655. {
  656. text = SearchText;
  657. }
  658. else
  659. {
  660. text = FormatValue(newItem, true);
  661. }
  662. // Update the Text property and the TextBox values
  663. UpdateTextValue(text);
  664. // Move the caret to the end of the text box
  665. if (TextBox != null && Text != null)
  666. {
  667. TextBox.SelectionStart = Text.Length;
  668. }
  669. }
  670. #endregion public object SelectedItem
  671. #region public string Text
  672. /// <summary>
  673. /// Gets or sets the text in the text box portion of the
  674. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.
  675. /// </summary>
  676. /// <value>The text in the text box portion of the
  677. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> control.</value>
  678. public string Text
  679. {
  680. get { return GetValue(TextProperty) as string; }
  681. set { SetValue(TextProperty, value); }
  682. }
  683. /// <summary>
  684. /// Identifies the
  685. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.Text" />
  686. /// dependency property.
  687. /// </summary>
  688. /// <value>The identifier for the
  689. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.Text" />
  690. /// dependency property.</value>
  691. public static readonly DependencyProperty TextProperty =
  692. DependencyProperty.Register(
  693. "Text",
  694. typeof(string),
  695. typeof(AutoCompleteBox),
  696. new PropertyMetadata(string.Empty, OnTextPropertyChanged));
  697. /// <summary>
  698. /// TextProperty property changed handler.
  699. /// </summary>
  700. /// <param name="d">AutoCompleteBox that changed its Text.</param>
  701. /// <param name="e">Event arguments.</param>
  702. private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  703. {
  704. AutoCompleteBox source = d as AutoCompleteBox;
  705. source.TextUpdated((string)e.NewValue, false);
  706. }
  707. #endregion public string Text
  708. #region public string SearchText
  709. /// <summary>
  710. /// Gets the text that is used to filter items in the
  711. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  712. /// item collection.
  713. /// </summary>
  714. /// <value>The text that is used to filter items in the
  715. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  716. /// item collection.</value>
  717. /// <remarks>
  718. /// The SearchText value is typically the same as the
  719. /// Text property, but is set after the TextChanged event occurs
  720. /// and before the Populating event.
  721. /// </remarks>
  722. public string SearchText
  723. {
  724. get { return (string)GetValue(SearchTextProperty); }
  725. private set
  726. {
  727. try
  728. {
  729. _allowWrite = true;
  730. SetValue(SearchTextProperty, value);
  731. }
  732. finally
  733. {
  734. _allowWrite = false;
  735. }
  736. }
  737. }
  738. /// <summary>
  739. /// Identifies the
  740. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.SearchText" />
  741. /// dependency property.
  742. /// </summary>
  743. /// <value>The identifier for the
  744. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.SearchText" />
  745. /// dependency property.</value>
  746. public static readonly DependencyProperty SearchTextProperty =
  747. DependencyProperty.Register(
  748. "SearchText",
  749. typeof(string),
  750. typeof(AutoCompleteBox),
  751. new PropertyMetadata(string.Empty, OnSearchTextPropertyChanged));
  752. /// <summary>
  753. /// OnSearchTextProperty property changed handler.
  754. /// </summary>
  755. /// <param name="d">AutoCompleteBox that changed its SearchText.</param>
  756. /// <param name="e">Event arguments.</param>
  757. private static void OnSearchTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  758. {
  759. AutoCompleteBox source = d as AutoCompleteBox;
  760. if (source._ignorePropertyChange)
  761. {
  762. source._ignorePropertyChange = false;
  763. return;
  764. }
  765. // Ensure the property is only written when expected
  766. if (!source._allowWrite)
  767. {
  768. // Reset the old value before it was incorrectly written
  769. source._ignorePropertyChange = true;
  770. source.SetValue(e.Property, e.OldValue);
  771. //throw new InvalidOperationException(Properties.Resources.AutoComplete_OnSearchTextPropertyChanged_InvalidWrite);
  772. }
  773. }
  774. #endregion public string SearchText
  775. #region public AutoCompleteFilterMode FilterMode
  776. /// <summary>
  777. /// Gets or sets how the text in the text box is used to filter items
  778. /// specified by the
  779. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  780. /// property for display in the drop-down.
  781. /// </summary>
  782. /// <value>One of the
  783. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteFilterMode" />
  784. /// values The default is
  785. /// <see cref="F:Microsoft.Phone.Controls.AutoCompleteFilterMode.StartsWith" />.</value>
  786. /// <exception cref="T:System.ArgumentException">The specified value is
  787. /// not a valid
  788. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteFilterMode" />.</exception>
  789. /// <remarks>
  790. /// Use the FilterMode property to specify how possible matches are
  791. /// filtered. For example, possible matches can be filtered in a
  792. /// predefined or custom way. The search mode is automatically set to
  793. /// Custom if you set the ItemFilter property.
  794. /// </remarks>
  795. public AutoCompleteFilterMode FilterMode
  796. {
  797. get { return (AutoCompleteFilterMode)GetValue(FilterModeProperty); }
  798. set { SetValue(FilterModeProperty, value); }
  799. }
  800. /// <summary>
  801. /// Gets the identifier for the
  802. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.FilterMode" />
  803. /// dependency property.
  804. /// </summary>
  805. public static readonly DependencyProperty FilterModeProperty =
  806. DependencyProperty.Register(
  807. "FilterMode",
  808. typeof(AutoCompleteFilterMode),
  809. typeof(AutoCompleteBox),
  810. new PropertyMetadata(AutoCompleteFilterMode.StartsWith, OnFilterModePropertyChanged));
  811. /// <summary>
  812. /// FilterModeProperty property changed handler.
  813. /// </summary>
  814. /// <param name="d">AutoCompleteBox that changed its FilterMode.</param>
  815. /// <param name="e">Event arguments.</param>
  816. [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "The exception will be thrown when the CLR setter is used in most situations.")]
  817. private static void OnFilterModePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  818. {
  819. AutoCompleteBox source = d as AutoCompleteBox;
  820. AutoCompleteFilterMode mode = (AutoCompleteFilterMode)e.NewValue;
  821. if (mode != AutoCompleteFilterMode.Contains &&
  822. mode != AutoCompleteFilterMode.ContainsCaseSensitive &&
  823. mode != AutoCompleteFilterMode.ContainsOrdinal &&
  824. mode != AutoCompleteFilterMode.ContainsOrdinalCaseSensitive &&
  825. mode != AutoCompleteFilterMode.Custom &&
  826. mode != AutoCompleteFilterMode.Equals &&
  827. mode != AutoCompleteFilterMode.EqualsCaseSensitive &&
  828. mode != AutoCompleteFilterMode.EqualsOrdinal &&
  829. mode != AutoCompleteFilterMode.EqualsOrdinalCaseSensitive &&
  830. mode != AutoCompleteFilterMode.None &&
  831. mode != AutoCompleteFilterMode.StartsWith &&
  832. mode != AutoCompleteFilterMode.StartsWithCaseSensitive &&
  833. mode != AutoCompleteFilterMode.StartsWithOrdinal &&
  834. mode != AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive)
  835. {
  836. source.SetValue(e.Property, e.OldValue);
  837. //throw new ArgumentException(Properties.Resources.AutoComplete_OnFilterModePropertyChanged_InvalidValue, "value");
  838. }
  839. // Sets the filter predicate for the new value
  840. AutoCompleteFilterMode newValue = (AutoCompleteFilterMode)e.NewValue;
  841. source.TextFilter = AutoCompleteSearch.GetFilter(newValue);
  842. }
  843. #endregion public AutoCompleteFilterMode FilterMode
  844. #region public AutoCompleteFilterPredicate ItemFilter
  845. /// <summary>
  846. /// Gets or sets the custom method that uses user-entered text to filter
  847. /// the items specified by the
  848. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  849. /// property for display in the drop-down.
  850. /// </summary>
  851. /// <value>The custom method that uses the user-entered text to filter
  852. /// the items specified by the
  853. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  854. /// property. The default is null.</value>
  855. /// <remarks>
  856. /// The filter mode is automatically set to Custom if you set the
  857. /// ItemFilter property.
  858. /// </remarks>
  859. public AutoCompleteFilterPredicate<object> ItemFilter
  860. {
  861. get { return GetValue(ItemFilterProperty) as AutoCompleteFilterPredicate<object>; }
  862. set { SetValue(ItemFilterProperty, value); }
  863. }
  864. /// <summary>
  865. /// Identifies the
  866. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemFilter" />
  867. /// dependency property.
  868. /// </summary>
  869. /// <value>The identifier for the
  870. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemFilter" />
  871. /// dependency property.</value>
  872. public static readonly DependencyProperty ItemFilterProperty =
  873. DependencyProperty.Register(
  874. "ItemFilter",
  875. typeof(AutoCompleteFilterPredicate<object>),
  876. typeof(AutoCompleteBox),
  877. new PropertyMetadata(OnItemFilterPropertyChanged));
  878. /// <summary>
  879. /// ItemFilterProperty property changed handler.
  880. /// </summary>
  881. /// <param name="d">AutoCompleteBox that changed its ItemFilter.</param>
  882. /// <param name="e">Event arguments.</param>
  883. private static void OnItemFilterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  884. {
  885. AutoCompleteBox source = d as AutoCompleteBox;
  886. AutoCompleteFilterPredicate<object> value = e.NewValue as AutoCompleteFilterPredicate<object>;
  887. // If null, revert to the "None" predicate
  888. if (value == null)
  889. {
  890. source.FilterMode = AutoCompleteFilterMode.None;
  891. }
  892. else
  893. {
  894. source.FilterMode = AutoCompleteFilterMode.Custom;
  895. source.TextFilter = null;
  896. }
  897. }
  898. #endregion public AutoCompleteFilterPredicate ItemFilter
  899. #region public AutoCompleteStringFilterPredicate TextFilter
  900. /// <summary>
  901. /// Gets or sets the custom method that uses the user-entered text to
  902. /// filter items specified by the
  903. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  904. /// property in a text-based way for display in the drop-down.
  905. /// </summary>
  906. /// <value>The custom method that uses the user-entered text to filter
  907. /// items specified by the
  908. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.ItemsSource" />
  909. /// property in a text-based way for display in the drop-down.</value>
  910. /// <remarks>
  911. /// The search mode is automatically set to Custom if you set the
  912. /// TextFilter property.
  913. /// </remarks>
  914. public AutoCompleteFilterPredicate<string> TextFilter
  915. {
  916. get { return GetValue(TextFilterProperty) as AutoCompleteFilterPredicate<string>; }
  917. set { SetValue(TextFilterProperty, value); }
  918. }
  919. /// <summary>
  920. /// Identifies the
  921. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.TextFilter" />
  922. /// dependency property.
  923. /// </summary>
  924. /// <value>The identifier for the
  925. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.TextFilter" />
  926. /// dependency property.</value>
  927. public static readonly DependencyProperty TextFilterProperty =
  928. DependencyProperty.Register(
  929. "TextFilter",
  930. typeof(AutoCompleteFilterPredicate<string>),
  931. typeof(AutoCompleteBox),
  932. new PropertyMetadata(AutoCompleteSearch.GetFilter(AutoCompleteFilterMode.StartsWith)));
  933. #endregion public AutoCompleteStringFilterPredicate TextFilter
  934. #if WINDOWS_PHONE
  935. #region public InputScope InputScope
  936. /// <summary>
  937. /// Gets or sets the
  938. /// <see cref="T:System.Windows.Input.InputScope"/>
  939. /// used by the Text template part.
  940. /// </summary>
  941. public InputScope InputScope
  942. {
  943. get { return (InputScope)GetValue(InputScopeProperty); }
  944. set { SetValue(InputScopeProperty, value); }
  945. }
  946. /// <summary>
  947. /// Identifies the
  948. /// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.InputScope"/>
  949. /// dependency property.
  950. /// </summary>
  951. public static readonly DependencyProperty InputScopeProperty =
  952. DependencyProperty.Register(
  953. "InputScope",
  954. typeof(InputScope),
  955. typeof(AutoCompleteBox),
  956. null);
  957. #endregion public InputScope InputScope
  958. #endif
  959. #region Template parts
  960. /// <summary>
  961. /// Gets or sets the drop down popup control.
  962. /// </summary>
  963. private PopupHelper DropDownPopup { get; set; }
  964. /// <summary>
  965. /// Determines whether text completion should be done.
  966. /// </summary>
  967. private static bool IsCompletionEnabled
  968. {
  969. get
  970. {
  971. PhoneApplicationFrame frame;
  972. return PhoneHelper.TryGetPhoneApplicationFrame(out frame) && frame.IsPortrait();
  973. }
  974. }
  975. /// <summary>
  976. /// The TextBox template part.
  977. /// </summary>
  978. private TextBox _text;
  979. /// <summary>
  980. /// The SelectionAdapter.
  981. /// </summary>
  982. private ISelectionAdapter _adapter;
  983. /// <summary>
  984. /// Gets or sets the Text template part.
  985. /// </summary>
  986. internal TextBox TextBox
  987. {
  988. get { return _text; }
  989. set
  990. {
  991. // Detach existing handlers
  992. if (_text != null)
  993. {
  994. _text.SelectionChanged -= OnTextBoxSelectionChanged;
  995. _text.TextChanged -= OnTextBoxTextChanged;
  996. }
  997. _text = value;
  998. // Attach handlers
  999. if (_text != null)
  1000. {
  1001. _text.SelectionChanged += OnTextBoxSelectionChanged;
  1002. _text.TextChanged += OnTextBoxTextChanged;
  1003. if (Text != null)
  1004. {
  1005. UpdateTextValue(Text);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. /// <summary>
  1011. /// Gets or sets the selection adapter used to populate the drop-down
  1012. /// with a list of selectable items.
  1013. /// </summary>
  1014. /// <value>The selection adapter used to populate the drop-down with a
  1015. /// list of selectable items.</value>
  1016. /// <remarks>
  1017. /// You can use this property when you create an automation peer to
  1018. /// use with AutoCompleteBox or deriving from AutoCompleteBox to
  1019. /// create a custom control.
  1020. /// </remarks>
  1021. protected internal ISelectionAdapter SelectionAdapter
  1022. {
  1023. get { return _adapter; }
  1024. set
  1025. {
  1026. if (_adapter != null)
  1027. {
  1028. _adapter.SelectionChanged -= OnAdapterSelectionChanged;
  1029. _adapter.Commit -= OnAdapterSelectionComplete;
  1030. _adapter.Cancel -= OnAdapterSelectionCanceled;
  1031. _adapter.Cancel -= OnAdapterSelectionComplete;
  1032. _adapter.ItemsSource = null;
  1033. }
  1034. _adapter = value;
  1035. if (_adapter != null)
  1036. {
  1037. _adapter.SelectionChanged += OnAdapterSelectionChanged;
  1038. _adapter.Commit += OnAdapterSelectionComplete;
  1039. _adapter.Cancel += OnAdapterSelectionCanceled;
  1040. _adapter.Cancel += OnAdapterSelectionComplete;
  1041. _adapter.ItemsSource = _view;
  1042. }
  1043. }
  1044. }
  1045. #endregion
  1046. /// <summary>
  1047. /// Occurs when the text in the text box portion of the
  1048. /// <see cref="T:Microsoft.Phone.Controls.AutoCompleteBox" /> changes.

Large files files are truncated, but you can click here to view the full file