Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Text;
- namespace Revolucent.Dasso.Program.Binding
- {
- static class Binding
- {
- public static void SetBinding<S, T, P>(this S source, Expression<Func<S, P>> sourceGetterExpression, T target, Action<T, P> targetSetter)
- where S : INotifyPropertyChanged
- where T : INotifyPropertyChanged
- {
- var sourceGetter = sourceGetterExpression.Compile();
- var memberExpression = (MemberExpression)sourceGetterExpression.Body;
- var propertyName = memberExpression.Member.Name;
- source.PropertyChanged += (sender, e) =>
- {
- if (e.PropertyName != propertyName)
- return;
- targetSetter(target, sourceGetter(source));
- };
- }
- public static void SetBinding<S, T, P>(this S source, Expression<Func<S, P>> sourceGetterExpression, T target, Expression<Func<T, P>> targetGetterExpression)
- where S : INotifyPropertyChanged
- where T : INotifyPropertyChanged
- {
- var memberExpression = (MemberExpression)targetGetterExpression.Body;
- var propertyInfo = (PropertyInfo)memberExpression.Member;
- Action<T, P> targetSetter = (targ, value) => propertyInfo.SetValue(targ, value, null);
- SetBinding(source, sourceGetterExpression, target, targetSetter);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement