// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details. // All other rights reserved. using System.Windows.Data; namespace System.Windows.Controls { /// /// A framework element that permits a binding to be evaluated in a new data /// context leaf node. /// /// The type of dynamic binding to return. internal partial class BindingEvaluator : FrameworkElement { /// /// Gets or sets the string value binding used by the control. /// private Binding _binding; #region public T Value /// /// Gets or sets the data item string value. /// public T Value { get { return (T)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } /// /// Identifies the Value dependency property. /// public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(T), typeof(BindingEvaluator), new PropertyMetadata(default(T))); #endregion public string Value /// /// Gets or sets the value binding. /// public Binding ValueBinding { get { return _binding; } set { _binding = value; SetBinding(ValueProperty, _binding); } } /// /// Initializes a new instance of the BindingEvaluator class. /// public BindingEvaluator() { } /// /// Initializes a new instance of the BindingEvaluator class, /// setting the initial binding to the provided parameter. /// /// The initial string value binding. public BindingEvaluator(Binding binding) { SetBinding(ValueProperty, binding); } /// /// Clears the data context so that the control does not keep a /// reference to the last-looked up item. /// public void ClearDataContext() { DataContext = null; } /// /// Updates the data context of the framework element and returns the /// updated binding value. /// /// The object to use as the data context. /// If set to true, this parameter will /// clear the data context immediately after retrieving the value. /// Returns the evaluated T value of the bound dependency /// property. public T GetDynamicValue(object o, bool clearDataContext) { DataContext = o; T value = Value; if (clearDataContext) { DataContext = null; } return value; } /// /// Updates the data context of the framework element and returns the /// updated binding value. /// /// The object to use as the data context. /// Returns the evaluated T value of the bound dependency /// property. public T GetDynamicValue(object o) { DataContext = o; return Value; } } }