FuncDataTemplate.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reflection;
  5. namespace Perspex.Controls.Templates
  6. {
  7. /// <summary>
  8. /// Builds a control for a piece of data.
  9. /// </summary>
  10. public class FuncDataTemplate : FuncTemplate<object, IControl>, IDataTemplate
  11. {
  12. /// <summary>
  13. /// The default data template used in the case where not matching data template is found.
  14. /// </summary>
  15. public static readonly FuncDataTemplate Default =
  16. new FuncDataTemplate(typeof(object), o => (o != null) ? new TextBlock { Text = o.ToString() } : null);
  17. /// <summary>
  18. /// The implementation of the <see cref="Match"/> method.
  19. /// </summary>
  20. private readonly Func<object, bool> _match;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="FuncDataTemplate"/> class.
  23. /// </summary>
  24. /// <param name="type">The type of data which the data template matches.</param>
  25. /// <param name="build">
  26. /// A function which when passed an object of <paramref name="type"/> returns a control.
  27. /// </param>
  28. public FuncDataTemplate(Type type, Func<object, IControl> build)
  29. : this(o => IsInstance(o, type), build)
  30. {
  31. }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="FuncDataTemplate"/> class.
  34. /// </summary>
  35. /// <param name="match">
  36. /// A function which determines whether the data template matches the specified data.
  37. /// </param>
  38. /// <param name="build">
  39. /// A function which returns a control for matching data.
  40. /// </param>
  41. public FuncDataTemplate(Func<object, bool> match, Func<object, IControl> build)
  42. : base(build)
  43. {
  44. Contract.Requires<ArgumentNullException>(match != null);
  45. _match = match;
  46. }
  47. /// <summary>
  48. /// Checks to see if this data template matches the specified data.
  49. /// </summary>
  50. /// <param name="data">The data.</param>
  51. /// <returns>
  52. /// True if the data template can build a control for the data, otherwise false.
  53. /// </returns>
  54. public bool Match(object data)
  55. {
  56. return _match(data);
  57. }
  58. /// <summary>
  59. /// Determines of an object is of the specified type.
  60. /// </summary>
  61. /// <param name="o">The object.</param>
  62. /// <param name="t">The type.</param>
  63. /// <returns>
  64. /// True if <paramref name="o"/> is of type <paramref name="t"/>, otherwise false.
  65. /// </returns>
  66. private static bool IsInstance(object o, Type t)
  67. {
  68. return (o != null) ?
  69. t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) :
  70. false;
  71. }
  72. }
  73. }