Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Text;
  8.  
  9. namespace Revolucent.Dasso.Program.Binding
  10. {
  11.     static class Binding
  12.     {
  13.         public static void SetBinding<S, T, P>(this S source, Expression<Func<S, P>> sourceGetterExpression, T target, Action<T, P> targetSetter)
  14.             where S : INotifyPropertyChanged
  15.             where T : INotifyPropertyChanged
  16.         {
  17.             var sourceGetter = sourceGetterExpression.Compile();
  18.             var memberExpression = (MemberExpression)sourceGetterExpression.Body;
  19.             var propertyName = memberExpression.Member.Name;
  20.             source.PropertyChanged += (sender, e) =>
  21.                 {
  22.                     if (e.PropertyName != propertyName)
  23.                         return;
  24.                     targetSetter(target, sourceGetter(source));
  25.                 };
  26.         }
  27.  
  28.         public static void SetBinding<S, T, P>(this S source, Expression<Func<S, P>> sourceGetterExpression, T target, Expression<Func<T, P>> targetGetterExpression)
  29.             where S : INotifyPropertyChanged
  30.             where T : INotifyPropertyChanged
  31.         {
  32.             var memberExpression = (MemberExpression)targetGetterExpression.Body;
  33.             var propertyInfo = (PropertyInfo)memberExpression.Member;
  34.             Action<T, P> targetSetter = (targ, value) => propertyInfo.SetValue(targ, value, null);
  35.             SetBinding(source, sourceGetterExpression, target, targetSetter);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement