public object ConvertBack(object value, Type targetType)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            var valueConverter = ValueConverterProvider.Get <TValueConverter>();
            var convertedValue = BindingValue.UnsetValue;

            if (TryGetParameter(out var parameter))
            {
                try
                {
                    convertedValue = valueConverter.ConvertBack(value, targetType, parameter, _converterCulture);
                }
                catch (Exception ex)
                {
                    Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while converting \"{value ?? "null"}\" value from the target item to the source one " +
                        $"using \"{TypeFormatter.FormatName<TValueConverter>()}\" value converter.");
                }
            }

            return(convertedValue);
        }
Example #2
0
        protected void SetTargetItemValue([NotNull] TTargetItem targetItem, [CanBeNull] object targetItemValue)
        {
            if (targetItemValue == BindingValue.UnsetValue)
            {
                if (FallbackValue != null)
                {
                    targetItemValue = FallbackValue.Value;
                }
                else
                {
                    if (!SourceItemBinding.TryGetItem(out _))
                    {
                        Log("Source item is null and fallback value is not set. " +
                            "This may lead to diplaying value from the previous source item. " +
                            "Set fallback value to override previous one.");
                    }
                }
            }

            if (targetItemValue != BindingValue.UnsetValue)
            {
                try
                {
                    TargetItemBinding.SetValue(targetItem, (TTargetItemValue)targetItemValue, _compositeItemBindingMode == BindingMode.TwoWay);
                }
                catch (Exception ex)
                {
                    Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                        $"\"{LogFormatter.FormatTypeName(TargetItemBinding)}.{nameof(TargetItemBinding.SetValue)}\" method.");
                }
            }
        }
Example #3
0
 internal SourceItemEmptyBinding(
     [NotNull] ItemReference <TItem> itemReference,
     [NotNull] Type sourceItemBindingType,
     [CanBeNull] LambdaExpression itemValue,
     [NotNull] Exception ex)
     : base(itemReference, () => LogFormatter.FormatExpression(itemValue))
 {
     Log($"\"{LogFormatter.FormatException(ex)}\" exception occurred while creating " +
         $"\"{LogFormatter.FormatTypeName(sourceItemBindingType)}\" instance.");
 }
Example #4
0
        protected bool TryGetTargetItemValue([NotNull] TTargetItem targetItem, out TTargetItemValue targetItemValue)
        {
            try
            {
                return(TargetItemBinding.TryGetValue(targetItem, out targetItemValue));
            }
            catch (Exception ex)
            {
                Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                    $"\"{LogFormatter.FormatTypeName(TargetItemBinding)}.{nameof(TargetItemBinding.TryGetValue)}\" method.");

                targetItemValue = default;

                return(false);
            }
        }
Example #5
0
        protected bool TryGetSourceItemValue([NotNull] TSourceItem sourceItem, out TSourceItemValue sourceItemValue)
        {
            try
            {
                return(SourceItemBinding.TryGetValue(sourceItem, out sourceItemValue));
            }
            catch (Exception ex)
            {
                Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                    $"\"{LogFormatter.FormatTypeName(SourceItemBinding)}.{nameof(SourceItemBinding.TryGetValue)}\" method.");

                sourceItemValue = default;

                return(false);
            }
        }
Example #6
0
        protected override void UnsubscribeFromSourceItemEvents()
        {
            if (SourceItemBinding.TryGetItem(out var sourceItem))
            {
                try
                {
                    SourceItemBinding.UnsubscribeFromEvents(sourceItem);
                }
                catch (Exception ex)
                {
                    Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                        $"\"{LogFormatter.FormatTypeName(SourceItemBinding)}.{nameof(SourceItemBinding.UnsubscribeFromEvents)}\" method.");
                }

                SourceItemBinding.CanExecuteChanged -= SourceItemCommandBinding_CanExecuteChanged;
            }
        }
Example #7
0
        internal object ConvertBack([CanBeNull] object value, [NotNull] Type targetType, [CanBeNull] object parameter)
        {
            var valueConverter = ValueConverterFactory.GetOrCreate(_converterType);

            try
            {
                return(valueConverter.ConvertBack(value, targetType, parameter, _converterCulture));
            }
            catch (Exception ex)
            {
                Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while converting \"{value ?? "null"}\" value from the target item to the source one " +
                    $"using \"{LogFormatter.FormatTypeName(_converterType)}\" value converter. This might reduce binding performance. " +
                    $"Please return \"{nameof(BindingValue)}.{nameof(BindingValue.UnsetValue)}\" value instead of rising an exception.");

                return(BindingValue.UnsetValue);
            }
        }
        protected void SetSourceItemValue([NotNull] TSourceItem sourceItem, [CanBeNull] TTargetItemValue targetItemValue)
        {
            var sourceItemValue = ValueConverter.ConvertBack(targetItemValue, typeof(TSourceItemValue));

            if (sourceItemValue != BindingValue.UnsetValue)
            {
                try
                {
                    SourceItemBinding.SetValue(sourceItem, (TSourceItemValue)sourceItemValue, _compositeItemBindingMode == BindingMode.TwoWay);
                }
                catch (Exception ex)
                {
                    Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                        $"\"{LogFormatter.FormatTypeName(SourceItemBinding)}.{nameof(SourceItemBinding.SetValue)}\" method.");
                }
            }
        }
Example #9
0
 private void UnsubscribeFromTargetItemEvents()
 {
     if (TargetItemBinding.TryGetItem(out var targetItem))
     {
         if (IsFromTargetToSourceBindingMode())
         {
             try
             {
                 TargetItemBinding.UnsubscribeFromEvents(targetItem);
             }
             catch (Exception ex)
             {
                 Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                     $"\"{LogFormatter.FormatTypeName(TargetItemBinding)}.{nameof(TargetItemBinding.UnsubscribeFromEvents)}\" method.");
             }
         }
     }
 }
Example #10
0
 protected override void UnsubscribeFromSourceItemEvents()
 {
     if (SourceItemBinding.TryGetItem(out var sourceItem))
     {
         if (IsFromSourceToTargetBindingMode(includingOneTimeBindingMode: false))
         {
             try
             {
                 SourceItemBinding.UnsubscribeFromEvents(sourceItem);
             }
             catch (Exception ex)
             {
                 Log($"An \"{LogFormatter.FormatException(ex)}\" exception occurred while executing " +
                     $"\"{LogFormatter.FormatTypeName(SourceItemBinding)}.{nameof(SourceItemBinding.UnsubscribeFromEvents)}\" method.");
             }
         }
     }
 }