Exemplo n.º 1
0
        /// <include file='doc\ListControl.uex' path='docs/doc[@for="ListControl.GetItemText"]/*' />
        public string GetItemText(object item)
        {
            // !formattingEnabled == RTM behaviour
            if (!formattingEnabled)
            {
                // Microsoft gave his blessing to this RTM breaking change
                if (item == null)
                {
                    return(String.Empty);
                }

                item = FilterItemOnProperty(item, displayMember.BindingField);
                return((item != null) ? Convert.ToString(item, CultureInfo.CurrentCulture) : "");
            }

            //
            // Whidbey formatting features
            //

            object filteredItem = FilterItemOnProperty(item, displayMember.BindingField);

            // first try: the OnFormat event
            ListControlConvertEventArgs e = new ListControlConvertEventArgs(filteredItem, typeof(String), item);

            OnFormat(e);

            // Microsoft: we need a better check. Should add the Handled property on the ListControlConvertEventArgs?
            if (e.Value != item && e.Value is String)
            {
                return((string)e.Value);
            }

            // try Formatter::FormatObject
            if (stringTypeConverter == null)
            {
                stringTypeConverter = TypeDescriptor.GetConverter(typeof(String));
            }
            try
            {
                return((string)Formatter.FormatObject(filteredItem, typeof(String), this.DisplayMemberConverter, stringTypeConverter, formatString, formatInfo, null, System.DBNull.Value));
            }
            catch (Exception exception)
            {
                if (ClientUtils.IsSecurityOrCriticalException(exception))
                {
                    throw;
                }
                // if we did not do any work then return the old ItemText
                return((filteredItem != null) ? Convert.ToString(item, CultureInfo.CurrentCulture) : "");
            }
        }
        private object FormatObject(object value)
        {
            if (this.ControlAtDesignTime())
            {
                return(value);
            }
            System.Type propertyType = this.propInfo.PropertyType;
            if (this.formattingEnabled)
            {
                ConvertEventArgs args = new ConvertEventArgs(value, propertyType);
                this.OnFormat(args);
                if (args.Value != value)
                {
                    return(args.Value);
                }
                TypeConverter sourceConverter = null;
                if (this.bindToObject.FieldInfo != null)
                {
                    sourceConverter = this.bindToObject.FieldInfo.Converter;
                }
                return(Formatter.FormatObject(value, propertyType, sourceConverter, this.propInfoConverter, this.formatString, this.formatInfo, this.nullValue, this.dsNullValue));
            }
            ConvertEventArgs cevent = new ConvertEventArgs(value, propertyType);

            this.OnFormat(cevent);
            object obj2 = cevent.Value;

            if (propertyType == typeof(object))
            {
                return(value);
            }
            if ((obj2 != null) && (obj2.GetType().IsSubclassOf(propertyType) || (obj2.GetType() == propertyType)))
            {
                return(obj2);
            }
            TypeConverter converter2 = TypeDescriptor.GetConverter((value != null) ? value.GetType() : typeof(object));

            if ((converter2 != null) && converter2.CanConvertTo(propertyType))
            {
                return(converter2.ConvertTo(value, propertyType));
            }
            if (value is IConvertible)
            {
                obj2 = Convert.ChangeType(value, propertyType, CultureInfo.CurrentCulture);
                if ((obj2 != null) && (obj2.GetType().IsSubclassOf(propertyType) || (obj2.GetType() == propertyType)))
                {
                    return(obj2);
                }
            }
            throw new FormatException(System.Windows.Forms.SR.GetString("ListBindingFormatFailed"));
        }
Exemplo n.º 3
0
        public string GetItemText(object item)
        {
            if (!this.formattingEnabled)
            {
                if (item == null)
                {
                    return(string.Empty);
                }
                item = this.FilterItemOnProperty(item, this.displayMember.BindingField);
                if (item == null)
                {
                    return("");
                }
                return(Convert.ToString(item, CultureInfo.CurrentCulture));
            }
            object obj2 = this.FilterItemOnProperty(item, this.displayMember.BindingField);
            ListControlConvertEventArgs e = new ListControlConvertEventArgs(obj2, typeof(string), item);

            this.OnFormat(e);
            if ((e.Value != item) && (e.Value is string))
            {
                return((string)e.Value);
            }
            if (stringTypeConverter == null)
            {
                stringTypeConverter = TypeDescriptor.GetConverter(typeof(string));
            }
            try
            {
                return((string)Formatter.FormatObject(obj2, typeof(string), this.DisplayMemberConverter, stringTypeConverter, this.formatString, this.formatInfo, null, DBNull.Value));
            }
            catch (Exception exception)
            {
                if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
                {
                    throw;
                }
                return((obj2 != null) ? Convert.ToString(item, CultureInfo.CurrentCulture) : "");
            }
        }
Exemplo n.º 4
0
        private object FormatObject(object value)
        {
            // We will not format the object when the control is in design time.
            // This is because if we bind a boolean property on a control to a
            // row that is full of DBNulls then we cause problems in the shell.
            if (ControlAtDesignTime())
            {
                return(value);
            }

            Type type = propInfo.PropertyType;

            if (formattingEnabled)
            {
                // -------------------------------
                // Behavior for Whidbey and beyond
                // -------------------------------

                // Fire the Format event so that user code gets a chance to supply the formatted value for us
                ConvertEventArgs e = new ConvertEventArgs(value, type);
                OnFormat(e);

                if (e.Value != value)
                {
                    // If event handler replaced parsed value with formatted value, use that
                    return(e.Value);
                }
                else
                {
                    // Otherwise format the parsed value ourselves
                    TypeConverter fieldInfoConverter = null;
                    if (bindToObject.FieldInfo != null)
                    {
                        fieldInfoConverter = bindToObject.FieldInfo.Converter;
                    }
                    return(Formatter.FormatObject(value, type, fieldInfoConverter, propInfoConverter, formatString, formatInfo, nullValue, dsNullValue));
                }
            }
            else
            {
                // ----------------------------
                // Behavior for RTM and Everett  [DO NOT MODIFY!]
                // ----------------------------

                // first try: use the Format event
                ConvertEventArgs e = new ConvertEventArgs(value, type);
                OnFormat(e);
                object ret = e.Value;

                // Approved breaking-change behavior between RTM and Everett: Fire the Format event even if the control property is of type
                // Object (RTM used to skip the event for properties of this type).

                if (type == typeof(object))
                {
                    return(value);
                }

                // stop now if we have a value of a compatible type
                if (ret != null && (ret.GetType().IsSubclassOf(type) || ret.GetType() == type))
                {
                    return(ret);
                }
                // second try: use type converter for the desiredType
                TypeConverter typeConverter = TypeDescriptor.GetConverter(value != null ? value.GetType() : typeof(object));
                if (typeConverter != null && typeConverter.CanConvertTo(type))
                {
                    ret = typeConverter.ConvertTo(value, type);
                    return(ret);
                }
                // last try: use Convert.ChangeType
                if (value is IConvertible)
                {
                    ret = Convert.ChangeType(value, type, CultureInfo.CurrentCulture);
                    if (ret != null && (ret.GetType().IsSubclassOf(type) || ret.GetType() == type))
                    {
                        return(ret);
                    }
                }

                // time to fail:
                throw new FormatException(SR.ListBindingFormatFailed);
            }
        }