Exemplo n.º 1
0
        internal static ITypeEditor CreateDefaultEditor(Type propertyType, TypeConverter typeConverter, PropertyItem propertyItem)
        {
            ITypeEditor editor = null;

            var context = new EditorTypeDescriptorContext(null, propertyItem.Instance, propertyItem.PropertyDescriptor);

            if ((typeConverter != null) &&
                typeConverter.GetStandardValuesSupported(context) &&
                typeConverter.GetStandardValuesExclusive(context) &&
                (propertyType != typeof(bool)) && (propertyType != typeof(bool?)))   //Bool type always have a BooleanConverter with standardValues : True/False.
            {
                var items = typeConverter.GetStandardValues(context);
                editor = new SourceComboBoxEditor(items, typeConverter);
            }
            else if (propertyType == typeof(string))
            {
                editor = new TextBoxEditor();
            }
            else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
            {
                editor = new CheckBoxEditor();
            }
            else if (propertyType == typeof(decimal) || propertyType == typeof(decimal?))
            {
                editor = new DecimalUpDownEditor();
            }
            else if (propertyType == typeof(double) || propertyType == typeof(double?))
            {
                editor = new DoubleUpDownEditor();
            }
            else if (propertyType == typeof(int) || propertyType == typeof(int?))
            {
                editor = new IntegerUpDownEditor();
            }
            else if (propertyType == typeof(short) || propertyType == typeof(short?))
            {
                editor = new ShortUpDownEditor();
            }
            else if (propertyType == typeof(long) || propertyType == typeof(long?))
            {
                editor = new LongUpDownEditor();
            }
            else if (propertyType == typeof(float) || propertyType == typeof(float?))
            {
                editor = new SingleUpDownEditor();
            }
            else if (propertyType == typeof(byte) || propertyType == typeof(byte?))
            {
                editor = new ByteUpDownEditor();
            }
            else if (propertyType == typeof(sbyte) || propertyType == typeof(sbyte?))
            {
                editor = new SByteUpDownEditor();
            }
            else if (propertyType == typeof(uint) || propertyType == typeof(uint?))
            {
                editor = new UIntegerUpDownEditor();
            }
            else if (propertyType == typeof(ulong) || propertyType == typeof(ulong?))
            {
                editor = new ULongUpDownEditor();
            }
            else if (propertyType == typeof(ushort) || propertyType == typeof(ushort?))
            {
                editor = new UShortUpDownEditor();
            }
            else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
            {
                editor = new DateTimeUpDownEditor();
            }
            else if ((propertyType == typeof(Color)) || (propertyType == typeof(Color?)))
            {
                editor = new ColorEditor();
            }
            else if (propertyType.IsEnum)
            {
                editor = new EnumComboBoxEditor();
            }
            else if (propertyType == typeof(TimeSpan) || propertyType == typeof(TimeSpan?))
            {
                editor = new TimeSpanUpDownEditor();
            }
            else if (propertyType == typeof(FontFamily) || propertyType == typeof(FontWeight) || propertyType == typeof(FontStyle) || propertyType == typeof(FontStretch))
            {
                editor = new FontComboBoxEditor();
            }
            else if (propertyType == typeof(Guid) || propertyType == typeof(Guid?))
            {
                editor = new MaskedTextBoxEditor()
                {
                    ValueDataType = propertyType, Mask = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"
                }
            }
            ;
            else if (propertyType == typeof(char) || propertyType == typeof(char?))
            {
                editor = new MaskedTextBoxEditor()
                {
                    ValueDataType = propertyType, Mask = "&"
                }
            }
            ;
            else if (propertyType == typeof(object))
            {
                // If any type of object is possible in the property, default to the TextBoxEditor.
                // Useful in some case (e.g., Button.Content).
                // Can be reconsidered but was the legacy behavior on the PropertyGrid.
                editor = new TextBoxEditor();
            }
            else
            {
                var listType = ListUtilities.GetListItemType(propertyType);

                // A List of T
                if (listType != null)
                {
                    if (!listType.IsPrimitive && !listType.Equals(typeof(String)) && !listType.IsEnum)
                    {
                        editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.CollectionEditor();
                    }
                    else
                    {
                        editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.PrimitiveTypeCollectionEditor();
                    }
                }
                else
                {
                    var dictionaryType = ListUtilities.GetDictionaryItemsType(propertyType);
                    var collectionType = ListUtilities.GetCollectionItemType(propertyType);
                    // A dictionary of T or a Collection of T or an ICollection
                    if ((dictionaryType != null) || (collectionType != null) || typeof(ICollection).IsAssignableFrom(propertyType))
                    {
                        editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.CollectionEditor();
                    }
                    else
                    {
                        // If the type is not supported, check if there is a converter that supports
                        // string conversion to the object type. Use TextBox in theses cases.
                        // Otherwise, return a TextBlock editor since no valid editor exists.
                        editor = (typeConverter != null && typeConverter.CanConvertFrom(typeof(string)))
                          ? (ITypeEditor) new TextBoxEditor()
                          : (ITypeEditor) new TextBlockEditor();
                    }
                }
            }

            return(editor);
        }
        protected override void ResolveValueBinding(PropertyItem propertyItem)
        {
            var type = propertyItem.PropertyType;

            Editor.ItemsSourceType = type;

            if (propertyItem.DescriptorDefinition != null &&
                propertyItem.DescriptorDefinition.NewItemTypes != null &&
                propertyItem.DescriptorDefinition.NewItemTypes.Count > 0)
            {
                Editor.NewItemTypes = propertyItem.DescriptorDefinition.NewItemTypes;
            }
            else if (type.BaseType == typeof(System.Array))
            {
                Editor.NewItemTypes = new List <Type>()
                {
                    type.GetElementType()
                };
            }
            else
            {
                if ((propertyItem.DescriptorDefinition != null) &&
                    (propertyItem.DescriptorDefinition.NewItemTypes != null) &&
                    (propertyItem.DescriptorDefinition.NewItemTypes.Count > 0))
                {
                    Editor.NewItemTypes = propertyItem.DescriptorDefinition.NewItemTypes;
                }
                else
                {
                    //Check if we have a Dictionary
                    var dictionaryTypes = ListUtilities.GetDictionaryItemsType(type);
                    if ((dictionaryTypes != null) && (dictionaryTypes.Length == 2))
                    {
                        // A Dictionary contains KeyValuePair that can't be edited.
                        // We need to create EditableKeyValuePairs.
                        // Create a EditableKeyValuePair< TKey, TValue> type from dictionary generic arguments type
                        var editableKeyValuePairType = ListUtilities.CreateEditableKeyValuePairType(dictionaryTypes[0], dictionaryTypes[1]);
                        Editor.NewItemTypes = new List <Type>()
                        {
                            editableKeyValuePairType
                        };
                    }
                    else
                    {
                        //Check if we have a list
                        var listType = ListUtilities.GetListItemType(type);
                        if (listType != null)
                        {
                            Editor.NewItemTypes = new List <Type>()
                            {
                                listType
                            };
                        }
                        else
                        {
                            //Check if we have a Collection of T
                            var colType = ListUtilities.GetCollectionItemType(type);
                            if (colType != null)
                            {
                                Editor.NewItemTypes = new List <Type>()
                                {
                                    colType
                                };
                            }
                        }
                    }
                }
            }

            base.ResolveValueBinding(propertyItem);
        }