aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/HybridControl.cs
blob: 0d2fe5336e63e15cbfa76f34dbd03fd0f41f4cab (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
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.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tango.Core.Commands;

namespace Tango.Editors
{
    /// <summary>
    /// Represents abstract user control with some additional features.
    /// </summary>
    /// <seealso cref="System.Windows.Controls.UserControl" />
    /// <seealso cref="System.ComponentModel.INotifyPropertyChanged" />
    public class HybridControl : UserControl, INotifyPropertyChanged
    {
        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        protected virtual void RaisePropertyChanged(String propName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        /// <summary>
        /// Raises all relay commands CanExecute methods in the current instance.
        /// </summary>
        public virtual void InvalidateRelayCommands()
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                foreach (var prop in this.GetType().GetProperties().Where(x => x.PropertyType == typeof(RelayCommand)))
                {
                    var value = prop.GetValue(this) as RelayCommand;

                    if (value != null)
                    {
                        value.RaiseCanExecuteChanged();
                    }
                }
            }));
        }

        /// <exclude/>
        /// <summary> 
        /// Cast the instance of a dependency object to type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="d">The d.</param>
        /// <returns></returns>
        public static T GetInstance<T>(DependencyObject d) where T : DependencyObject
        {
            return d as T;
        }

        /// <summary>
        /// Occurs when a property value has changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }
}