aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/DependencyObjectExtensions.cs
blob: 9947f8e0cd24b6e5b12e01706996e85b9bb163c1 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;

/// <exclude/>
/// <summary>
/// Contains a collection of DependencyObject extension methods.
/// </summary>
public static class DependencyObjectExtensions
{
    /// <summary>
    /// Invokes the current dependency object dispatcher.
    /// </summary>
    /// <param name="dependencyObject">The dependency object.</param>
    /// <param name="action">The action.</param>
    internal static void BeginInvoke(this DependencyObject dependencyObject, Action action)
    {
        dependencyObject.Dispatcher.BeginInvoke(action);
    }

    /// <summary>
    /// Invokes the specified action.
    /// </summary>
    /// <param name="dependencyObject">The dependency object.</param>
    /// <param name="action">The action.</param>
    internal static void Invoke(this DependencyObject dependencyObject, Action action)
    {
        dependencyObject.Dispatcher.Invoke(action);
    }

    /// <summary>
    /// Determines whether this object is currently in design time mode.
    /// </summary>
    /// <param name="obj">The object.</param>
    /// <returns></returns>
    internal static bool IsInDesignMode(this DependencyObject obj)
    {
        return (DesignerProperties.GetIsInDesignMode(obj));
    }


    /// <summary>
    /// Binds the specified dependency property to a another object property.
    /// </summary>
    /// <param name="target">The target dependency object.</param>
    /// <param name="targetDP">The target dependency property.</param>
    /// <param name="source">The source object.</param>
    /// <param name="sourceDP">The source dependency property.</param>
    /// <param name="mode">Binding mode.</param>
    /// <param name="converter">Binding converter.</param>
    /// <returns></returns>
    public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, DependencyProperty sourceDP, BindingMode mode, IValueConverter converter = null)
    {
        Binding binding = new Binding();
        binding.Mode = mode;
        binding.Source = source;
        binding.Path = new PropertyPath(sourceDP);
        binding.Converter = converter;
        BindingOperations.SetBinding(target, targetDP, binding);
        return binding;
    }

    /// <summary>
    /// Binds the specified dependency property to a another object property.
    /// </summary>
    /// <param name="target">The target dependency object.</param>
    /// <param name="targetDP">The target dependency property.</param>
    /// <param name="source">The source object.</param>
    /// <param name="sourceDP">The source dependency property.</param>
    /// <param name="mode">Binding mode.</param>
    /// <returns></returns>
    public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, DependencyProperty sourceDP, BindingMode mode = BindingMode.Default)
    {
        Binding binding = new Binding();
        binding.Mode = mode;
        binding.Source = source;
        binding.Path = new PropertyPath(sourceDP);
        BindingOperations.SetBinding(target, targetDP, binding);
        return binding;
    }

    /// <summary>
    /// Create asynchronous binding between target and source.
    /// </summary>
    /// <param name="target">The target dependency object.</param>
    /// <param name="targetDP">The target dependency property.</param>
    /// <param name="source">The source object.</param>
    /// <param name="sourceDP">The source dependency property.</param>
    /// <param name="mode">Binding mode.</param>
    /// <returns></returns>
    public static Binding BindAsync(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, DependencyProperty sourceDP, BindingMode mode = BindingMode.Default)
    {
        Binding binding = new Binding();
        binding.Mode = mode;
        binding.Source = source;
        binding.Path = new PropertyPath(sourceDP);
        binding.IsAsync = true;
        binding.BindsDirectlyToSource = true;
        BindingOperations.SetBinding(target, targetDP, binding);
        return binding;
    }

    /// <summary>
    /// Binds the specified dependency property to a another object property.
    /// </summary>
    /// <param name="target">The target dependency object.</param>
    /// <param name="targetDP">The target dependency property.</param>
    /// <param name="source">The source object.</param>
    /// <param name="sourceDP">The source dependency property name.</param>
    /// <param name="mode">Binding mode.</param>
    /// <returns></returns>
    public static Binding Bind(this DependencyObject target, DependencyProperty targetDP, DependencyObject source, String sourceDP, BindingMode mode = BindingMode.Default)
    {
        Binding binding = new Binding();
        binding.Mode = mode;
        binding.Source = source;
        binding.Path = new PropertyPath(sourceDP);
        BindingOperations.SetBinding(target, targetDP, binding);
        return binding;
    }

    /// <summary>
    /// Clears bindings from the specified dependency property.
    /// </summary>
    /// <param name="target">The target dependency object.</param>
    /// <param name="dependencyProperty">The dependency property.</param>
    public static void Unbind(this DependencyObject target, DependencyProperty dependencyProperty)
    {
        BindingOperations.ClearBinding(target, dependencyProperty);
    }
}