BindingEvaluator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Windows.Data;
  6. namespace System.Windows.Controls
  7. {
  8. /// <summary>
  9. /// A framework element that permits a binding to be evaluated in a new data
  10. /// context leaf node.
  11. /// </summary>
  12. /// <typeparam name="T">The type of dynamic binding to return.</typeparam>
  13. internal partial class BindingEvaluator<T> : FrameworkElement
  14. {
  15. /// <summary>
  16. /// Gets or sets the string value binding used by the control.
  17. /// </summary>
  18. private Binding _binding;
  19. #region public T Value
  20. /// <summary>
  21. /// Gets or sets the data item string value.
  22. /// </summary>
  23. public T Value
  24. {
  25. get { return (T)GetValue(ValueProperty); }
  26. set { SetValue(ValueProperty, value); }
  27. }
  28. /// <summary>
  29. /// Identifies the Value dependency property.
  30. /// </summary>
  31. public static readonly DependencyProperty ValueProperty =
  32. DependencyProperty.Register(
  33. "Value",
  34. typeof(T),
  35. typeof(BindingEvaluator<T>),
  36. new PropertyMetadata(default(T)));
  37. #endregion public string Value
  38. /// <summary>
  39. /// Gets or sets the value binding.
  40. /// </summary>
  41. public Binding ValueBinding
  42. {
  43. get { return _binding; }
  44. set
  45. {
  46. _binding = value;
  47. SetBinding(ValueProperty, _binding);
  48. }
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the BindingEvaluator class.
  52. /// </summary>
  53. public BindingEvaluator()
  54. {
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the BindingEvaluator class,
  58. /// setting the initial binding to the provided parameter.
  59. /// </summary>
  60. /// <param name="binding">The initial string value binding.</param>
  61. public BindingEvaluator(Binding binding)
  62. {
  63. SetBinding(ValueProperty, binding);
  64. }
  65. /// <summary>
  66. /// Clears the data context so that the control does not keep a
  67. /// reference to the last-looked up item.
  68. /// </summary>
  69. public void ClearDataContext()
  70. {
  71. DataContext = null;
  72. }
  73. /// <summary>
  74. /// Updates the data context of the framework element and returns the
  75. /// updated binding value.
  76. /// </summary>
  77. /// <param name="o">The object to use as the data context.</param>
  78. /// <param name="clearDataContext">If set to true, this parameter will
  79. /// clear the data context immediately after retrieving the value.</param>
  80. /// <returns>Returns the evaluated T value of the bound dependency
  81. /// property.</returns>
  82. public T GetDynamicValue(object o, bool clearDataContext)
  83. {
  84. DataContext = o;
  85. T value = Value;
  86. if (clearDataContext)
  87. {
  88. DataContext = null;
  89. }
  90. return value;
  91. }
  92. /// <summary>
  93. /// Updates the data context of the framework element and returns the
  94. /// updated binding value.
  95. /// </summary>
  96. /// <param name="o">The object to use as the data context.</param>
  97. /// <returns>Returns the evaluated T value of the bound dependency
  98. /// property.</returns>
  99. public T GetDynamicValue(object o)
  100. {
  101. DataContext = o;
  102. return Value;
  103. }
  104. }
  105. }