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.Graphics2D { public class BindingEventContainer : DependencyObject { private DrawingVisual _visual; private FrameworkElement _element; public event EventHandler ValueChanged; 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())); public BindingEventContainer(DrawingVisual visual, FrameworkElement element) { _visual = visual; _element = element; } private void OnValueChanged() { ValueChanged?.Invoke(this, new BindingEventArgs() { Element = _element, Visual = _visual, Value = Value, }); } public static BindingEventContainer Generate(DrawingVisual visual, FrameworkElement element, DependencyProperty elementProperty) { BindingEventContainer container = new BindingEventContainer(visual, element); Binding binding = new Binding(); binding.Mode = BindingMode.OneWay; binding.Source = element; binding.Path = new PropertyPath(elementProperty); BindingOperations.SetBinding(container, BindingEventContainer.ValueProperty, binding); return container; } } }