Example #1
0
 private void UpdateValueIfNecessary()
 {
     if (this.listeningToLostFocus)
     {
         this.targetPropertyState = BindingExpression.TargetPropertyState.Dirty;
         if (this.IsTargetFocussed())
         {
             return;
         }
     }
     this.UpdateValue();
 }
Example #2
0
 private void UpdateValue()
 {
     Exception innerException = null;
     string dataErrorInfoMessage = null;
     CultureInfo cultureInfo = null;
     bool flag = false;
     if (this.listener == null || !this.listener.FullPathExists)
     {
         return;
     }
     this.updateState = BindingExpression.UpdateState.UpdatingSource;
     try
     {
         object value = this.targetDO.GetValue(this.targetProperty);
         if (this.binding.TargetNullValue != null)
         {
             object targetNullValue = null;
             try
             {
                 Type type = this.binding.TargetNullValue.GetType();
                 if (type == this.targetProperty.PropertyType || this.targetProperty.PropertyType.IsAssignableFrom(type))
                 {
                     targetNullValue = this.binding.TargetNullValue;
                 }
                 else
                 {
                     if (cultureInfo == null)
                     {
                         cultureInfo = this.CalculateCulture();
                     }
                     targetNullValue = this.DynamicConverter.Convert(this.binding.TargetNullValue, this.targetProperty.PropertyType, null, cultureInfo);
                 }
                 if (object.Equals(value, targetNullValue))
                 {
                     value = null;
                 }
             }
             catch (Exception e)
             {
                 Debug.WriteLine(e.ToString());
             }
         }
         if (this.binding.Converter != null)
         {
             if (cultureInfo == null)
             {
                 cultureInfo = this.CalculateCulture();
             }
             value = this.binding.Converter.ConvertBack(value, this.listener.LeafType, this.binding.ConverterParameter, cultureInfo);
             if (value == DependencyProperty.UnsetValue)
             {
                 return;
             }
         }
         if (value != null && value is string)
         {
             if (cultureInfo == null)
             {
                 cultureInfo = this.CalculateCulture();
             }
             object obj = BindingExpression.TryParse(value, this.listener.LeafType, cultureInfo);
             if (obj != DependencyProperty.UnsetValue)
             {
                 value = obj;
             }
         }
         try
         {
             if (!this.IsValidValueForUpdate(value, this.listener.LeafType))
             {
                 if (cultureInfo == null)
                 {
                     cultureInfo = this.CalculateCulture();
                 }
                 flag = true;
                 value = this.DynamicConverter.Convert(value, this.listener.LeafType, null, cultureInfo);
                 flag = false;
             }
             this.listener.LeafValue = value;
         }
         catch (TargetInvocationException targetInvocationException)
         {
             Debug.WriteLine(targetInvocationException.ToString());
             innerException = targetInvocationException.InnerException;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.ToString());
             if (e is OutOfMemoryException || e is StackOverflowException || e is AccessViolationException || e is ThreadAbortException)
             {
                 throw;
             }
         }
     }
     finally
     {
         if (this.binding.ValidatesOnDataErrors && (innerException == null || !this.binding.ValidatesOnExceptions))
         {
             dataErrorInfoMessage = this.GetDataErrorInfoMessage();
         }
         this.updateState = BindingExpression.UpdateState.None;
         this.targetPropertyState = BindingExpression.TargetPropertyState.Clean;
     }
     ValidationError validationError = null;
     if (innerException != null && this.binding.ValidatesOnExceptions)
     {
         validationError = new ValidationError(innerException, flag);
     }
     else if (!string.IsNullOrEmpty(dataErrorInfoMessage))
     {
         validationError = new ValidationError(dataErrorInfoMessage);
     }
     bool flag1 = innerException != null;
     if (flag1 && !this.lastSourceUpdateThrewException)
     {
         this.AddNotifyDataErrorInfosToQueue(ValidationErrorEventAction.Removed);
     }
     this.AddErrorNotificationToQueue(ValidationErrorEventAction.Removed, this.currentValidationError);
     if (!flag1 && this.lastSourceUpdateThrewException)
     {
         this.AddNotifyDataErrorInfosToQueue(ValidationErrorEventAction.Added);
     }
     this.AddErrorNotificationToQueue(ValidationErrorEventAction.Added, validationError);
     ValidationError validationError1 = this.currentValidationError;
     if (this.lastSourceUpdateThrewException && !flag1)
     {
         this.AddNotifyDataErrorInfosToTarget();
     }
     this.AddErrorToTarget(validationError, false);
     this.RemoveErrorFromTarget(validationError1);
     if (!this.lastSourceUpdateThrewException && flag1)
     {
         this.RemoveNotifyDataErrorInfosFromTarget();
     }
     this.lastSourceUpdateThrewException = flag1;
     this.RaiseErrorNotifications();
 }