diff options
Diffstat (limited to 'Software/Visual Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs')
| -rw-r--r-- | Software/Visual Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/Software/Visual Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs b/Software/Visual Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs new file mode 100644 index 000000000..16fd43728 --- /dev/null +++ b/Software/Visual Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs @@ -0,0 +1,137 @@ +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> + public 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> + public 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> + public 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); + } +} |
