/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2013 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.ComponentModel; using System.Reflection; namespace Greenshot.Drawing.Fields.Binding { /// /// Bidirectional binding of properties of two INotifyPropertyChanged instances. /// This implementation synchronizes null values, too. If you do not want this /// behavior (e.g. when binding to a /// public class BidirectionalBinding { private INotifyPropertyChanged controlObject; private INotifyPropertyChanged fieldObject; private string controlPropertyName; private string fieldPropertyName; private bool updatingControl = false; private bool updatingField = false; private IBindingConverter converter; private IBindingValidator validator; /// /// Whether or not null values are passed on to the other object. /// protected bool AllowSynchronizeNull = true; /// /// Bind properties of two objects bidirectionally /// /// Object containing 1st property to bind /// Property of 1st object to bind /// Object containing 2nd property to bind /// Property of 2nd object to bind public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName) { this.controlObject = controlObject; this.fieldObject = fieldObject; this.controlPropertyName = controlPropertyName; this.fieldPropertyName = fieldPropertyName; this.controlObject.PropertyChanged += ControlPropertyChanged; this.fieldObject.PropertyChanged += FieldPropertyChanged; } /// /// Bind properties of two objects bidirectionally, converting the values using a converter /// /// Object containing 1st property to bind /// Property of 1st object to bind /// Object containing 2nd property to bind /// Property of 2nd object to bind /// taking care of converting the synchronzied value to the correct target format and back public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingConverter converter) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName) { this.converter = converter; } /// /// Bind properties of two objects bidirectionally, converting the values using a converter. /// Synchronization can be intercepted by adding a validator. /// /// Object containing 1st property to bind /// Property of 1st object to bind /// Object containing 2nd property to bind /// Property of 2nd object to bind /// validator to intercept synchronisation if the value does not match certain criteria public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingValidator validator) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName) { this.validator = validator; } /// /// Bind properties of two objects bidirectionally, converting the values using a converter. /// Synchronization can be intercepted by adding a validator. /// /// Object containing 1st property to bind /// Property of 1st object to bind /// Object containing 2nd property to bind /// Property of 2nd object to bind /// taking care of converting the synchronzied value to the correct target format and back /// validator to intercept synchronisation if the value does not match certain criteria public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingConverter converter, IBindingValidator validator) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName, converter) { this.validator = validator; } public void ControlPropertyChanged(object sender, PropertyChangedEventArgs e) { if (!updatingControl && e.PropertyName.Equals(controlPropertyName)) { updatingField = true; synchronize(controlObject, controlPropertyName, fieldObject, fieldPropertyName); updatingField = false; } } public void FieldPropertyChanged(object sender, PropertyChangedEventArgs e) { if (!updatingField && e.PropertyName.Equals(fieldPropertyName)) { updatingControl = true; synchronize(fieldObject, fieldPropertyName, controlObject, controlPropertyName); updatingControl = false; } } private void synchronize(INotifyPropertyChanged sourceObject, string sourceProperty, INotifyPropertyChanged targetObject, string targetProperty) { PropertyInfo targetPropertyInfo = resolvePropertyInfo(targetObject, targetProperty); PropertyInfo sourcePropertyInfo = resolvePropertyInfo(sourceObject, sourceProperty); if (sourcePropertyInfo != null && targetPropertyInfo != null && targetPropertyInfo.CanWrite) { object bValue = sourcePropertyInfo.GetValue(sourceObject, null); if (converter != null && bValue != null) { bValue = converter.convert(bValue); } try { if (validator == null || validator.validate(bValue)) { targetPropertyInfo.SetValue(targetObject, bValue, null); } } catch (Exception e) { throw new MemberAccessException("Could not set property '" + targetProperty + "' to '" + bValue + "' [" + ((bValue != null) ? bValue.GetType().Name : "") + "] on " + targetObject + ". Probably other type than expected, IBindingCoverter to the rescue.", e); } } } private PropertyInfo resolvePropertyInfo(object obj, string property) { PropertyInfo ret = null; string[] properties = property.Split(".".ToCharArray()); for (int i = 0; i < properties.Length; i++) { string prop = properties[i]; ret = obj.GetType().GetProperty(prop); if (ret != null && ret.CanRead && i < prop.Length - 1) { obj = ret.GetValue(obj, null); } } return ret; } public IBindingConverter Converter { get { return converter; } set { converter = value; } } } }