aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/ExtensionMethods/INotifyPropertyChangedExtensions.cs
blob: 3bc1a91b17e14f54ad2fd1c43ad440191ecb1659 (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


public static class INotifyPropertyChangedExtensions
{
    /// <summary>
    /// Creates a property binding.
    /// </summary>
    /// <param name="target">The target.</param>
    /// <param name="targetPropName">Name of the target property.</param>
    /// <param name="source">The source.</param>
    /// <param name="sourcePropName">Name of the source property.</param>
    public static void Bind(this INotifyPropertyChanged target, String targetPropName, INotifyPropertyChanged source, String sourcePropName)
    {
        PropertyInfo targetPropInfo = target.GetType().GetProperty(targetPropName);
        PropertyInfo sourcePropInfo = source.GetType().GetProperty(sourcePropName);


        source.PropertyChanged += (x, e) => 
        {
            if (e.PropertyName == sourcePropName)
            {
                targetPropInfo.SetValue(target, sourcePropInfo.GetValue(source));
            }
        };
    }
}