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));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts multiple values with boolean representation to Visibility.
        /// </summary>
        /// <param name="values">The values to convert</param>
        /// <param name="targetType">The type of the binding target property. Only Visibility allowed.</param>
        /// <param name="parameter">
        ///     A string specifying the operator used in this conversion and the default value for invalid boolean values. "'operator'(,'default value')"
        ///     Supported operators are: "AND", "OR", default is "AND,Hidden"</param>
        /// <param name="culture">This parameter is ignored.</param>
        /// <returns>The Visibility value calculated from values</returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Debug.Assert(typeof(Visibility).IsAssignableFrom(targetType), "Only Visibility allowed as type of the binding target property");

            Visibility defaultVisibility = ParseVisibility(parameter);

            if (NullableBooleanConverter.Convert(values, defaultVisibility == Visibility.Visible, ParseOrOperator(parameter)))
            {
                return(Visibility.Visible);
            }
            else
            {
                return(InvisibleVisibility(ParseVisibility(parameter)));
            }
        }