using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace Tango.SharedUI
{
///
/// Represents a binding event container.
///
///
public class BindingEventContainer : DependencyObject
{
private Action _renewAction;
///
/// Occurs when the dependency property value has changed.
///
public event EventHandler ValueChanged;
///
/// Gets or sets the value.
///
public Object Value
{
get { return (Object)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(Object), typeof(BindingEventContainer), new PropertyMetadata(null, (d, e) => (d as BindingEventContainer).OnValueChanged()));
///
/// Gets or sets the binding property.
///
public BindingProperty BindingProperty { get; set; }
///
/// Gets or sets the dependency object.
///
public DependencyObject DependencyObject { get; set; }
///
/// Initializes a new instance of the class.
///
/// The dependency object.
/// The binding property.
public BindingEventContainer(DependencyObject dependencyObject, BindingProperty bindingProperty)
{
DependencyObject = dependencyObject;
BindingProperty = bindingProperty;
}
///
/// Called when the value has been changed
///
protected virtual void OnValueChanged()
{
ValueChanged?.Invoke(this, new BindingEventArgs()
{
BindingProperty = BindingProperty,
DependencyObject = DependencyObject,
Value = Value,
_renewAction = _renewAction
});
}
///
/// Generates a new for the specified Take element and binding property.
///
/// The take element.
/// The binding property.
///
public static BindingEventContainer Generate(DependencyObject dependencyObject, DependencyProperty dependencyProperty)
{
BindingEventContainer container = new BindingEventContainer(dependencyObject, new BindingProperty(dependencyProperty));
container._renewAction = () =>
{
Binding binding = new Binding();
binding.Mode = BindingMode.OneWay;
binding.Source = dependencyObject;
binding.Path = new PropertyPath(dependencyProperty);
BindingOperations.SetBinding(container, BindingEventContainer.ValueProperty, binding);
};
container._renewAction.Invoke();
return container;
}
}
}