Ejemplo n.º 1
0
 /// <summary>
 /// Converts a value into an object of type targetType.
 /// </summary>
 /// <param name="value">The value produced by the binding source.</param>
 /// <param name="targetType">The type of the binding target property.</param>
 /// <param name="parameter">The converter parameter, used as default value.</param>
 /// <param name="culture">The culture to use in the converter.</param>
 /// <returns>
 /// A converted value. If the method returns null, the valid null value is used.
 /// Returns Binding.DoNothing for unsupported targetType
 /// Returns DependencyProperty.UnsetValue if conversion fails.
 /// </returns>
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
     if (targetType == typeof(bool))
     {
         return(ConvertToBool(value, parameter));
     }
     else if (targetType == typeof(bool?))
     {
         return(ConvertToNullableBool(value, parameter));
     }
     else if (typeof(IConvertible).IsAssignableFrom(targetType))
     {
         bool?boolValue = NullableBooleanConverter.ConvertToNullableBool(value, parameter);
         try
         {
             return(System.Convert.ChangeType(boolValue, targetType, culture));
         }
         catch (InvalidCastException)
         {
             System.Diagnostics.Debug.WriteLine("{0}:ConvertBack failed '{1}' to '{2}', falling back to default '{3}'", this.GetType().Name, value, targetType, parameter);
             if (parameter == null)
             {
                 return(DependencyProperty.UnsetValue);
             }
             else
             {
                 return(this.Convert(parameter, targetType, null, culture));
             }
         }
     }
     else
     {
         return(Binding.DoNothing);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts a value with boolean representation to Visibility.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property. Only Visibility allowed.</param>
        /// <param name="parameter">The Visibility used if value has no boolean representation.</param>
        /// <param name="culture">This parameter is ignored.</param>
        /// <returns>The value converted to Visibility.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Debug.Assert(typeof(Visibility).IsAssignableFrom(targetType), this.GetType().Name + " Convert() supports only 'Visibility' as target type");
            bool?result = NullableBooleanConverter.ConvertToNullableBool(value, null);

            if (result != null && result.HasValue)
            {
                return(result.Value ? Visibility.Visible : InvisibleVisibility(ParseVisibility(parameter)));
            }
            else
            {
                return(ParseVisibility(parameter));
            }
        }