aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/TEMP/Tango.Graphics2D/BindingEventContainer.cs
blob: 759b5d0bc81bab7c491daf8522a59754da202254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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<BindingEventArgs> 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;
        }
    }
}