public static object EnumToObject(PropertyGridProperty property, object value)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (value != null && property.PropertyType.IsEnum)
            {
                return(Extensions.EnumToObject(property.PropertyType, value));
            }

            if (value != null && value.GetType().IsEnum)
            {
                return(Extensions.EnumToObject(value.GetType(), value));
            }

            if (property.PropertyType != typeof(string))
            {
                return(ConversionService.ChangeType(value, property.PropertyType));
            }

            var options = PropertyGridOptionsAttribute.FromProperty(property);

            if (options == null)
            {
                return(ConversionService.ChangeType(value, property.PropertyType));
            }

            return(EnumToObject(options, property.PropertyType, value));
        }
        protected virtual bool CollectionEditorHasOnlyOneColumn(PropertyGridProperty property)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            var att = PropertyGridOptionsAttribute.FromProperty(property);

            if (att != null)
            {
                return(att.CollectionEditorHasOnlyOneColumn);
            }

            if (_collectionEditorHasOnlyOneColumnList.Contains(property.CollectionItemPropertyType))
            {
                return(true);
            }

            return(!PropertyGridDataProvider.HasProperties(property.CollectionItemPropertyType));
        }
        protected virtual void OnSourcePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e == null || e.PropertyName == null)
            {
                return;
            }

            PropertyGridProperty property = GetProperty(e.PropertyName);

            if (property != null)
            {
                bool forceRaise = false;
                var  options    = PropertyGridOptionsAttribute.FromProperty(property);
                if (options != null)
                {
                    forceRaise = options.ForcePropertyChanged;
                }

                property.RefreshValueFromDescriptor(true, forceRaise, true);
                OnPropertyChanged(this, CreateEventArgs(property));
            }
        }
Esempio n. 4
0
        public virtual bool IsAssignableFrom(Type type, Type propertyType, PropertyGridDataTemplate template, PropertyGridProperty property)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (propertyType == null)
            {
                throw new ArgumentNullException("propertyType");
            }

            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (type.IsAssignableFrom(propertyType))
            {
                // bool? is assignable from bool, but we don't want that match
                if (!type.IsNullable() || propertyType.IsNullable())
                {
                    return(true);
                }
            }

            // hack for nullable enums...
            if (type == PropertyGridDataTemplate.NullableEnumType)
            {
                Type enumType;
                bool nullable;
                PropertyGridProperty.IsEnumOrNullableEnum(propertyType, out enumType, out nullable);
                if (nullable)
                {
                    return(true);
                }
            }

            var options = PropertyGridOptionsAttribute.FromProperty(property);

            if (options != null)
            {
                if ((type.IsEnum || type == typeof(Enum)) && options.IsEnum)
                {
                    if (!options.IsFlagsEnum)
                    {
                        return(true);
                    }

                    if (Extensions.IsFlagsEnum(type))
                    {
                        return(true);
                    }

                    if (template.IsFlagsEnum.HasValue && template.IsFlagsEnum.Value)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        public virtual IEnumerable BuildItems(PropertyGridProperty property, Type targetType, object parameter, CultureInfo culture)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            Type enumType;
            bool nullable;
            bool isEnumOrNullableEnum = PropertyGridProperty.IsEnumOrNullableEnum(property.PropertyType, out enumType, out nullable);

            PropertyGridItem zero = null;
            var att   = PropertyGridOptionsAttribute.FromProperty(property);
            var items = new ObservableCollection <PropertyGridItem>();

            if (isEnumOrNullableEnum)
            {
                if (nullable)
                {
                    PropertyGridItem item = CreateItem();
                    item.Property = property;
                    item.Name     = null; // "<unset>";
                    item.Value    = null;
                    item.IsUnset  = true;
                    items.Add(item);
                }

                string[] names  = Enum.GetNames(enumType);
                Array    values = Enum.GetValues(enumType);
                if (Extensions.IsFlagsEnum(enumType))
                {
                    ulong uvalue = EnumToUInt64(property, property.Value);

                    for (int i = 0; i < names.Length; i++)
                    {
                        string name      = names[i];
                        ulong  nameValue = EnumToUInt64(property, values.GetValue(i));
                        string displayName;
                        if (!ShowEnumField(property, enumType, names[i], out displayName))
                        {
                            continue;
                        }

                        PropertyGridItem item = CreateItem();
                        item.Property = property;
                        item.Name     = displayName;
                        item.Value    = nameValue;
                        item.IsZero   = nameValue == 0;
                        bool isChecked = true;

                        if (nameValue == 0)
                        {
                            zero = item;
                        }

                        // determine if this name is in fact a combination of other names
                        ulong bitsCount = (ulong)Extensions.GetEnumMaxPower(enumType) - 1; // skip first
                        ulong b         = 1;
                        for (ulong bit = 1; bit < bitsCount; bit++)                        // signed, skip highest bit
                        {
                            string bitName = Enum.GetName(enumType, b);
                            if (bitName != null && name != bitName && (nameValue & b) != 0)
                            {
                                if ((uvalue & b) == 0)
                                {
                                    isChecked = false;
                                }
                            }
                            b *= 2;
                        }

                        isChecked      = (uvalue & nameValue) != 0;
                        item.IsChecked = isChecked;
                        items.Add(item);
                    }

                    // determine if the lisbox is empty, which we don't want anyway
                    if (items.Count == 0)
                    {
                        PropertyGridItem item = CreateItem();
                        item.Property = property;
                        item.Name     = DefaultZeroName;
                        item.Value    = 0;
                        item.IsZero   = true;
                        items.Add(item);
                    }

                    if (uvalue == 0 && zero != null)
                    {
                        zero.IsChecked = true;
                    }
                }
                else
                {
                    for (int i = 0; i < names.Length; i++)
                    {
                        string displayName;
                        if (!ShowEnumField(property, enumType, names[i], out displayName))
                        {
                            continue;
                        }

                        PropertyGridItem item = CreateItem();
                        item.Property = property;
                        item.Name     = displayName;
                        item.Value    = values.GetValue(i);
                        item.IsZero   = i == 0; // first one is default
                        items.Add(item);
                    }
                }
            }
            else
            {
                if (att != null && att.IsEnum)
                {
                    bool manualFlags = false;
                    // either EnumList or EnumValues can be null but not both
                    // if not null, length must be the same
                    if (att.EnumNames == null || att.EnumNames.Length == 0)
                    {
                        if (att.EnumValues == null || att.EnumValues.Length == 0)
                        {
                            return(items);
                        }

                        att.EnumNames = new string[att.EnumValues.Length];
                        for (int i = 0; i < att.EnumValues.Length; i++)
                        {
                            att.EnumNames[i] = string.Format("{0}", att.EnumValues[i]);
                        }
                    }
                    else
                    {
                        if (att.EnumValues == null || att.EnumValues.Length != att.EnumNames.Length)
                        {
                            att.EnumValues = new object[att.EnumNames.Length];
                            if (att.IsFlagsEnum)
                            {
                                ulong current = 1; // don't use zero when nothing is specified in flags mode
                                manualFlags = true;
                                for (int i = 0; i < att.EnumNames.Length; i++)
                                {
                                    att.EnumValues[i] = current;
                                    current          *= 2;
                                }
                            }
                            else
                            {
                                for (int i = 0; i < att.EnumNames.Length; i++)
                                {
                                    att.EnumValues[i] = string.Format("{0}", att.EnumNames[i]);
                                }
                            }
                        }
                    }

                    // items value must of a compatible type with property.Value
                    Func <object, object> valueConverter = (v) =>
                    {
                        Type propType = property.Value != null?property.Value.GetType() : property.PropertyType;

                        if (v == null)
                        {
                            if (!propType.IsValueType)
                            {
                                return(null);
                            }

                            return(Activator.CreateInstance(propType));
                        }

                        Type vType = v.GetType();
                        if (propType.IsAssignableFrom(vType))
                        {
                            return(v);
                        }

                        return(ConversionService.ChangeType(v, propType));
                    };

                    if (att.IsFlagsEnum)
                    {
                        ulong uvalue = EnumToUInt64(property, property.Value);

                        for (int i = 0; i < att.EnumNames.Length; i++)
                        {
                            ulong nameValue = EnumToUInt64(property, att.EnumValues[i]);

                            PropertyGridItem item = CreateItem();
                            item.Property = property;
                            item.Name     = att.EnumNames[i];
                            item.Value    = valueConverter(att.EnumValues[i]);
                            if (manualFlags)
                            {
                                item.IsZero = i == 0;
                            }
                            else
                            {
                                item.IsZero = nameValue == 0;
                            }
                            bool isChecked = true;

                            if (nameValue == 0)
                            {
                                zero = item;
                            }

                            // note: in this case, we don't support names as a combination of other names
                            ulong bitsCount = (ulong)GetEnumMaxPower(att) - 1; // skip first
                            ulong b         = 1;
                            for (ulong bit = 1; bit < bitsCount; bit++)        // signed, skip highest bit
                            {
                                if ((uvalue & b) == 0)
                                {
                                    isChecked = false;
                                }
                                b *= 2;
                            }

                            isChecked      = (uvalue & nameValue) != 0;
                            item.IsChecked = isChecked;
                            items.Add(item);
                        }

                        // determine if the list is empty, which we don't want anyway
                        if (items.Count == 0)
                        {
                            PropertyGridItem item = CreateItem();
                            item.Property = property;
                            item.Name     = DefaultZeroName;
                            item.Value    = valueConverter(0);
                            item.IsZero   = true;
                            items.Add(item);
                        }

                        if (uvalue == 0 && zero != null)
                        {
                            zero.IsChecked = true;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < att.EnumNames.Length; i++)
                        {
                            PropertyGridItem item = CreateItem();
                            item.Property = property;
                            item.Name     = att.EnumNames[i];
                            item.Value    = valueConverter(att.EnumValues[i]);
                            item.IsZero   = i == 0; // first one is default
                            items.Add(item);
                        }
                    }
                }
            }

            var ctx = new Dictionary <string, object>();

            ctx["items"] = items;
            property.OnEvent(this, ActivatorService.CreateInstance <PropertyGridEventArgs>(property, ctx));
            return(items);
        }
        public static ulong EnumToUInt64(PropertyGridProperty property, object value)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (value == null)
            {
                return(0);
            }

            Type type = value.GetType();

            if (type.IsEnum)
            {
                return(Extensions.EnumToUInt64(value));
            }

            TypeCode typeCode = Convert.GetTypeCode(value);

            switch (typeCode)
            {
            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                return((ulong)Convert.ToInt64(value));

            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return(Convert.ToUInt64(value));
            }

            var att = PropertyGridOptionsAttribute.FromProperty(property);

            if (att == null || att.EnumNames == null)
            {
                return(0);
            }

            string svalue = string.Format("{0}", value);
            ulong  ul;

            if (ulong.TryParse(svalue, out ul))
            {
                return(ul);
            }

            var enums = ParseEnum(svalue);

            if (enums.Count == 0)
            {
                return(0);
            }

            foreach (string name in enums)
            {
                int index = IndexOf(att.EnumNames, name);
                if (index < 0)
                {
                    continue;
                }

                ulong ulvalue = Extensions.EnumToUInt64(att.EnumValues[index]);
                ul |= ulvalue;
            }
            return(ul);
        }
        protected virtual Window GetEditor(PropertyGridProperty property, object parameter)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            string resourceKey = string.Format("{0}", parameter);

            if (string.IsNullOrWhiteSpace(resourceKey))
            {
                var att = PropertyGridOptionsAttribute.FromProperty(property);
                if (att != null)
                {
                    resourceKey = att.EditorResourceKey;
                }

                if (string.IsNullOrWhiteSpace(resourceKey))
                {
                    resourceKey = property.DefaultEditorResourceKey;
                    if (string.IsNullOrWhiteSpace(resourceKey))
                    {
                        resourceKey = "ObjectEditorWindow";
                    }
                }
            }

            var editor = TryFindResource(resourceKey) as Window;

            if (editor != null)
            {
                editor.Owner = this.GetVisualSelfOrParent <Window>();
                if (editor.Owner != null)
                {
                    PropertyGridWindowOptions wo = PropertyGridWindowManager.GetOptions(editor);
                    if ((wo & PropertyGridWindowOptions.UseDefinedSize) == PropertyGridWindowOptions.UseDefinedSize)
                    {
                        if (double.IsNaN(editor.Left))
                        {
                            editor.Left = editor.Owner.Left + ChildEditorWindowOffset;
                        }

                        if (double.IsNaN(editor.Top))
                        {
                            editor.Top = editor.Owner.Top + ChildEditorWindowOffset;
                        }

                        if (double.IsNaN(editor.Width))
                        {
                            editor.Width = editor.Owner.Width;
                        }

                        if (double.IsNaN(editor.Height))
                        {
                            editor.Height = editor.Owner.Height;
                        }
                    }
                    else
                    {
                        editor.Left   = editor.Owner.Left + ChildEditorWindowOffset;
                        editor.Top    = editor.Owner.Top + ChildEditorWindowOffset;
                        editor.Width  = editor.Owner.Width;
                        editor.Height = editor.Owner.Height;
                    }
                }
                editor.DataContext = property;
                Selector selector = LogicalTreeHelper.FindLogicalNode(editor, "EditorSelector") as Selector;
                if (selector != null)
                {
                    selector.SelectedIndex = 0;
                }

                Grid grid = LogicalTreeHelper.FindLogicalNode(editor, "CollectionEditorListGrid") as Grid;
                if (grid != null && grid.ColumnDefinitions.Count > 2)
                {
                    if (property.IsCollection && CollectionEditorHasOnlyOneColumn(property))
                    {
                        grid.ColumnDefinitions[1].Width = new GridLength(0, GridUnitType.Pixel);
                        grid.ColumnDefinitions[2].Width = new GridLength(0, GridUnitType.Pixel);
                    }
                    else
                    {
                        grid.ColumnDefinitions[1].Width = new GridLength(5, GridUnitType.Pixel);
                        grid.ColumnDefinitions[2].Width = new GridLength(1, GridUnitType.Star);
                    }
                }

                var pge = editor as IPropertyGridEditor;
                if (pge != null)
                {
                    if (!pge.SetContext(property, parameter))
                    {
                        return(null);
                    }
                }
            }
            return(editor);
        }