Exemplo n.º 1
0
        /// <summary>
        /// Update a property from the target (.NET object) to the source (ViewModel). This method is used when in a TwoWay binding, to update the source property.
        /// </summary>
        /// <typeparam name="T">The type of the source property.</typeparam>
        /// <param name="sourceProperty">The source property to update.</param>
        /// <param name="viewModel">The ViewModel on which the property will be updated.</param>
        /// <param name="value">The new value to set the source property.</param>
        /// <param name="valueConverter">The converter to use to convert back the target (.NET object) property to the source (ViewModel) property.</param>
        /// <param name="converterParameter">The optional paramater to use in the value converter.</param>
        private static void UpdateSourceProperty <T>(PropertyInfo sourceProperty, BindableObject viewModel, T value, IBindingValueConverter valueConverter, string converterParameter)
        {
            _busyBindings.Add(sourceProperty.Name);

            sourceProperty.SetValue(viewModel, valueConverter == null ? value : valueConverter.ConvertBack(value, sourceProperty.PropertyType, converterParameter, CultureInfo.CurrentCulture));

            _busyBindings.Remove(sourceProperty.Name);
        }
        /// <summary>
        /// Update a property from the target (.NET object) to the source (ViewModel). This method is used when in a TwoWay binding, to update the source property.
        /// </summary>
        /// <typeparam name="T">The type of the source property.</typeparam>
        /// <param name="sourceProperty">The source property to update.</param>
        /// <param name="viewModel">The ViewModel on which the property will be updated.</param>
        /// <param name="value">The new value to set the source property.</param>
        /// <param name="valueConverter">The converter to use to convert back the target (.NET object) property to the source (ViewModel) property.</param>
        /// <param name="converterParameter">The optional paramater to use in the value converter.</param>
        private static void UpdateSourceProperty <T>(PropertyInfo sourceProperty, BindableObject viewModel, T value, IBindingValueConverter valueConverter, string converterParameter)
        {
            _preventUpdateForTargetProperty = true;

            sourceProperty.SetValue(viewModel, valueConverter == null ? value : valueConverter.ConvertBack(value, sourceProperty.PropertyType, converterParameter, CultureInfo.CurrentCulture));

            _preventUpdateForTargetProperty = false;
        }
Exemplo n.º 3
0
        private object GetTargetValue(IBindingMemberInfo targetMember, IDataContext context, bool throwOnError)
        {
            var    isDebuggable = IsDebuggable;
            object value        = GetRawValueInternal(targetMember, context, throwOnError);

            if (isDebuggable)
            {
                DebugInfo($"Got a target value: '{value}'");
            }
            if (value.IsUnsetValueOrDoNothing())
            {
                return(value);
            }

            IBindingValueConverter converter = _parameters.ConverterDelegate?.Invoke(context);

            if (converter != null)
            {
                CultureInfo culture   = _parameters.ConverterCultureDelegate?.Invoke(context) ?? BindingServiceProvider.BindingCultureInfo();
                object      parameter = _parameters.ConverterParameterDelegate?.Invoke(context);
                if (isDebuggable)
                {
                    DebugInfo($"Applying converter for target value: '{value}', converter: '{converter}', parameter: '{parameter}', culture: {culture}, target type: '{targetMember.Type}'");
                    value = converter.ConvertBack(value, targetMember.Type, parameter, culture, context);
                    DebugInfo($"Converter '{converter}' returns value: '{value}'");
                }
                else
                {
                    value = converter.ConvertBack(value, targetMember.Type, parameter, culture, context);
                }
            }

            if (Equals(value, _parameters.TargetNullValue))
            {
                if (isDebuggable)
                {
                    DebugInfo("Target value equals to TargetNullValue, return null value");
                }
                return(null);
            }
            return(value);
        }
        private object GetTargetValue(IBindingMemberInfo targetMember, IDataContext context, bool throwOnError)
        {
            object value = GetRawValueInternal(targetMember, context, throwOnError);

            if (value.IsUnsetValueOrDoNothing())
            {
                return(value);
            }
            if (_parameters.ConverterDelegate != null)
            {
                IBindingValueConverter converter = _parameters.ConverterDelegate(context);
                if (converter != null)
                {
                    CultureInfo culture   = _parameters.ConverterCultureDelegate.GetValueOrDefault(context, BindingServiceProvider.BindingCultureInfo());
                    object      parameter = _parameters.ConverterParameterDelegate.GetValueOrDefault(context);
                    value = converter.ConvertBack(value, targetMember.Type, parameter, culture, context);
                }
            }
            if (Equals(value, _parameters.TargetNullValue))
            {
                return(null);
            }
            return(value);
        }