Example #1
0
        /// <summary>
        /// Sends the current binding target value to the binding source property in TwoWay or OneWayToSource bindings.
        /// </summary>
        public void UpdateSource()
        {
            if (realMode != BindingMode.OneWayToSource && realMode != BindingMode.TwoWay)
            {
                throw new ApplicationException(String.Format("Cannot update source in {0} binding mode.", realMode));
            }
            ignoreSourceListener = true;
            try {
                Object targetValue;
                if (null == adapter)
                {
                    targetValue = targetPropertyInfo.GetGetMethod( ).Invoke(target, null);
                }
                else
                {
                    targetValue = adapter.GetValue(target, targetProperty);
                }
                //
                if (targetIsObservable)     // Work with collection
                {
                    IList sourceListNow = ( IList )sourcePropertyInfo.GetGetMethod().Invoke(source, null);
                    if (targetValue == null)
                    {
                        if (null != sourceListNow)
                        {
                            sourceListNow.Clear();
                        }
                    }
                    else
                    {
                        if (null != sourceListNow)
                        {
                            sourceListNow.Clear();
                            foreach (object item in (IEnumerable)targetValue)
                            {
                                sourceListNow.Add(item);
                            }

                            // Subscribe
                            if (targetList != null)
                            {
                                ((IObservableList)targetList).ListChanged -= targetListChanged;
                            }
                            targetList = (IList)targetValue;
                            sourceList = sourceListNow;
                            ((IObservableList)targetList).ListChanged += targetListChanged;
                        }
                        else
                        {
                            // Nothing to do : source list is null, ignoring sync operation
                        }
                    }
                }
                else     // Work with usual property
                {
                    Object convertedValue = targetValue;
                    // Convert if need
                    if (null != converter)
                    {
                        ConversionResult result = converter.Convert(targetValue);
                        if (!result.Success)
                        {
                            if (null != OnBinding)
                            {
                                OnBinding.Invoke(new BindingResult(true, false, result.FailReason));
                            }
                            if (updateSourceIfBindingFails)
                            {
                                // Will update source using null or default(T) if T is primitive
                                sourcePropertyInfo.GetSetMethod().Invoke(source, new object[] { null });
                            }
                            return;
                        }
                        convertedValue = result.Value;
                    }
                    // Validate if need
                    if (null != Validator)
                    {
                        ValidationResult validationResult = Validator.Validate(convertedValue);
                        if (!validationResult.Valid)
                        {
                            if (null != OnBinding)
                            {
                                OnBinding.Invoke(new BindingResult(false, true, validationResult.Message));
                            }
                            if (updateSourceIfBindingFails)
                            {
                                // Will update source using null or default(T) if T is primitive
                                sourcePropertyInfo.GetSetMethod().Invoke(source, new object[] { null });
                            }
                            return;
                        }
                    }
                    sourcePropertyInfo.GetSetMethod().Invoke(source, new object[] { convertedValue });
                    if (null != OnBinding)
                    {
                        OnBinding.Invoke(new BindingResult(false));
                    }
                    //
                }
            } finally {
                ignoreSourceListener = false;
            }
        }
 public ConversionResult ConvertBack(object tSecond)
 {
     return(converter.Convert(tSecond));
 }