/WindowsPhone7/Microsoft.Phone.Controls.Toolkit/AutoCompleteBox/AutoCompleteFilter.cs

https://github.com/kvervo/HorizontalLoopingSelector · C# · 207 lines · 93 code · 27 blank · 87 comment · 1 complexity · a916a1eb3b37d44e5c6b288512170368 MD5 · raw 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. #if WINDOWS_PHONE
  7. namespace Microsoft.Phone.Controls
  8. #else
  9. namespace System.Windows.Controls
  10. #endif
  11. {
  12. /// <summary>
  13. /// A predefined set of filter functions for the known, built-in
  14. /// AutoCompleteFilterMode enumeration values.
  15. /// </summary>
  16. internal static class AutoCompleteSearch
  17. {
  18. /// <summary>
  19. /// Index function that retrieves the filter for the provided
  20. /// AutoCompleteFilterMode.
  21. /// </summary>
  22. /// <param name="FilterMode">The built-in search mode.</param>
  23. /// <returns>Returns the string-based comparison function.</returns>
  24. public static AutoCompleteFilterPredicate<string> GetFilter(AutoCompleteFilterMode FilterMode)
  25. {
  26. switch (FilterMode)
  27. {
  28. case AutoCompleteFilterMode.Contains:
  29. return Contains;
  30. case AutoCompleteFilterMode.ContainsCaseSensitive:
  31. return ContainsCaseSensitive;
  32. case AutoCompleteFilterMode.ContainsOrdinal:
  33. return ContainsOrdinal;
  34. case AutoCompleteFilterMode.ContainsOrdinalCaseSensitive:
  35. return ContainsOrdinalCaseSensitive;
  36. case AutoCompleteFilterMode.Equals:
  37. return Equals;
  38. case AutoCompleteFilterMode.EqualsCaseSensitive:
  39. return EqualsCaseSensitive;
  40. case AutoCompleteFilterMode.EqualsOrdinal:
  41. return EqualsOrdinal;
  42. case AutoCompleteFilterMode.EqualsOrdinalCaseSensitive:
  43. return EqualsOrdinalCaseSensitive;
  44. case AutoCompleteFilterMode.StartsWith:
  45. return StartsWith;
  46. case AutoCompleteFilterMode.StartsWithCaseSensitive:
  47. return StartsWithCaseSensitive;
  48. case AutoCompleteFilterMode.StartsWithOrdinal:
  49. return StartsWithOrdinal;
  50. case AutoCompleteFilterMode.StartsWithOrdinalCaseSensitive:
  51. return StartsWithOrdinalCaseSensitive;
  52. case AutoCompleteFilterMode.None:
  53. case AutoCompleteFilterMode.Custom:
  54. default:
  55. return null;
  56. }
  57. }
  58. /// <summary>
  59. /// Check if the string value begins with the text.
  60. /// </summary>
  61. /// <param name="text">The AutoCompleteBox prefix text.</param>
  62. /// <param name="value">The item's string value.</param>
  63. /// <returns>Returns true if the condition is met.</returns>
  64. public static bool StartsWith(string text, string value)
  65. {
  66. return value.StartsWith(text, StringComparison.CurrentCultureIgnoreCase);
  67. }
  68. /// <summary>
  69. /// Check if the string value begins with the text.
  70. /// </summary>
  71. /// <param name="text">The AutoCompleteBox prefix text.</param>
  72. /// <param name="value">The item's string value.</param>
  73. /// <returns>Returns true if the condition is met.</returns>
  74. public static bool StartsWithCaseSensitive(string text, string value)
  75. {
  76. return value.StartsWith(text, StringComparison.CurrentCulture);
  77. }
  78. /// <summary>
  79. /// Check if the string value begins with the text.
  80. /// </summary>
  81. /// <param name="text">The AutoCompleteBox prefix text.</param>
  82. /// <param name="value">The item's string value.</param>
  83. /// <returns>Returns true if the condition is met.</returns>
  84. public static bool StartsWithOrdinal(string text, string value)
  85. {
  86. return value.StartsWith(text, StringComparison.OrdinalIgnoreCase);
  87. }
  88. /// <summary>
  89. /// Check if the string value begins with the text.
  90. /// </summary>
  91. /// <param name="text">The AutoCompleteBox prefix text.</param>
  92. /// <param name="value">The item's string value.</param>
  93. /// <returns>Returns true if the condition is met.</returns>
  94. public static bool StartsWithOrdinalCaseSensitive(string text, string value)
  95. {
  96. return value.StartsWith(text, StringComparison.Ordinal);
  97. }
  98. /// <summary>
  99. /// Check if the prefix is contained in the string value. The current
  100. /// culture's case insensitive string comparison operator is used.
  101. /// </summary>
  102. /// <param name="text">The AutoCompleteBox prefix text.</param>
  103. /// <param name="value">The item's string value.</param>
  104. /// <returns>Returns true if the condition is met.</returns>
  105. public static bool Contains(string text, string value)
  106. {
  107. return value.Contains(text, StringComparison.CurrentCultureIgnoreCase);
  108. }
  109. /// <summary>
  110. /// Check if the prefix is contained in the string value.
  111. /// </summary>
  112. /// <param name="text">The AutoCompleteBox prefix text.</param>
  113. /// <param name="value">The item's string value.</param>
  114. /// <returns>Returns true if the condition is met.</returns>
  115. public static bool ContainsCaseSensitive(string text, string value)
  116. {
  117. return value.Contains(text, StringComparison.CurrentCulture);
  118. }
  119. /// <summary>
  120. /// Check if the prefix is contained in the string value.
  121. /// </summary>
  122. /// <param name="text">The AutoCompleteBox prefix text.</param>
  123. /// <param name="value">The item's string value.</param>
  124. /// <returns>Returns true if the condition is met.</returns>
  125. public static bool ContainsOrdinal(string text, string value)
  126. {
  127. return value.Contains(text, StringComparison.OrdinalIgnoreCase);
  128. }
  129. /// <summary>
  130. /// Check if the prefix is contained in the string value.
  131. /// </summary>
  132. /// <param name="text">The AutoCompleteBox prefix text.</param>
  133. /// <param name="value">The item's string value.</param>
  134. /// <returns>Returns true if the condition is met.</returns>
  135. public static bool ContainsOrdinalCaseSensitive(string text, string value)
  136. {
  137. return value.Contains(text, StringComparison.Ordinal);
  138. }
  139. /// <summary>
  140. /// Check if the string values are equal.
  141. /// </summary>
  142. /// <param name="text">The AutoCompleteBox prefix text.</param>
  143. /// <param name="value">The item's string value.</param>
  144. /// <returns>Returns true if the condition is met.</returns>
  145. public static bool Equals(string text, string value)
  146. {
  147. return value.Equals(text, StringComparison.CurrentCultureIgnoreCase);
  148. }
  149. /// <summary>
  150. /// Check if the string values are equal.
  151. /// </summary>
  152. /// <param name="text">The AutoCompleteBox prefix text.</param>
  153. /// <param name="value">The item's string value.</param>
  154. /// <returns>Returns true if the condition is met.</returns>
  155. public static bool EqualsCaseSensitive(string text, string value)
  156. {
  157. return value.Equals(text, StringComparison.CurrentCulture);
  158. }
  159. /// <summary>
  160. /// Check if the string values are equal.
  161. /// </summary>
  162. /// <param name="text">The AutoCompleteBox prefix text.</param>
  163. /// <param name="value">The item's string value.</param>
  164. /// <returns>Returns true if the condition is met.</returns>
  165. public static bool EqualsOrdinal(string text, string value)
  166. {
  167. return value.Equals(text, StringComparison.OrdinalIgnoreCase);
  168. }
  169. /// <summary>
  170. /// Check if the string values are equal.
  171. /// </summary>
  172. /// <param name="text">The AutoCompleteBox prefix text.</param>
  173. /// <param name="value">The item's string value.</param>
  174. /// <returns>Returns true if the condition is met.</returns>
  175. public static bool EqualsOrdinalCaseSensitive(string text, string value)
  176. {
  177. return value.Equals(text, StringComparison.Ordinal);
  178. }
  179. }
  180. }