internal FrameworkElement GenerateChildrenEditorElement(PropertyItem propertyItem) { FrameworkElement editorElement = null; DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition; object definitionKey = null; Type definitionKeyAsType = definitionKey as Type; ITypeEditor editor = null; if (editor == null) { editor = pd.CreateAttributeEditor(); } if (editor != null) { editorElement = editor.ResolveEditor(propertyItem); } if (editorElement == null && definitionKey == null) { editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyDescriptor.Name, propertyItem); } if (editorElement == null && definitionKeyAsType == null) { editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyType, propertyItem); } foreach (var edef in this.PropertyContainer.EditorDefinitions.Where(x => x.HasAttribute != null)) { if (propertyItem.PropertyDescriptor.Attributes.Cast <Attribute>().Any(x => x.GetType() == edef.HasAttribute)) { return(edef.GenerateEditingElementInternal(propertyItem)); } } if (editorElement == null) { if (pd.IsReadOnly) { editor = new TextBlockEditor(); } // Fallback: Use a default type editor. if (editor == null) { editor = (definitionKeyAsType != null) ? PropertyGridUtilities.CreateDefaultEditor(definitionKeyAsType, null) : pd.CreateDefaultEditor(); } Debug.Assert(editor != null); editorElement = editor.ResolveEditor(propertyItem); } return(editorElement); }
internal FrameworkElement GenerateChildrenEditorElement(PropertyItem propertyItem) { FrameworkElement editorElement = null; DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition; object definitionKey = null; Type definitionKeyAsType = definitionKey as Type; ITypeEditor editor = null; if (editor == null) { editor = pd.CreateAttributeEditor(); } if (editor != null) { editorElement = editor.ResolveEditor(propertyItem); } if ((editorElement == null) && (definitionKey == null) && (propertyItem.PropertyDescriptor != null)) { editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyDescriptor.Name, propertyItem); } if (editorElement == null && definitionKeyAsType == null) { editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyType, propertyItem); } if (editorElement == null) { if (propertyItem.IsReadOnly && !ListUtilities.IsListOfItems(propertyItem.PropertyType) && !ListUtilities.IsCollectionOfItems(propertyItem.PropertyType) && !ListUtilities.IsDictionaryOfItems(propertyItem.PropertyType)) { editor = new TextBlockEditor((propertyItem.PropertyDescriptor != null) ? propertyItem.PropertyDescriptor.Converter : null); } // Fallback: Use a default type editor. if (editor == null) { editor = (definitionKeyAsType != null) ? PropertyGridUtilities.CreateDefaultEditor(definitionKeyAsType, null, propertyItem) : pd.CreateDefaultEditor(propertyItem); } Debug.Assert(editor != null); editorElement = editor.ResolveEditor(propertyItem); } return(editorElement); }
private FrameworkElement GenerateChildrenEditorElement(PropertyItem propertyItem) { FrameworkElement editorElement = null; DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition; object definitionKey = null; Type definitionKeyAsType = definitionKey as Type; ITypeEditor editor = null; if (pd.IsReadOnly) { editor = new TextBlockEditor(); } if (editor == null) { editor = pd.CreateAttributeEditor(); } if (editor != null) { editorElement = editor.ResolveEditor(propertyItem); } if (editorElement == null && definitionKey == null) { editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyDescriptor.Name, propertyItem); } if (editorElement == null && definitionKeyAsType == null) { editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyType, propertyItem); } if (editorElement == null) { // Fallback: Use a default type editor. if (editor == null) { editor = (definitionKeyAsType != null) ? PropertyGridUtilities.CreateDefaultEditor(definitionKeyAsType, null) : pd.CreateDefaultEditor(); } Debug.Assert(editor != null); if (editor != null) { editorElement = editor.ResolveEditor(propertyItem); } } return(editorElement); }
public FrameworkElement GenerateEditorElement(PropertyItem propertyItem) { FrameworkElement editorElement = null; // Priority #1: CustomEditors based on the Attribute. ITypeEditor editor = GetAttributeEditor(); if (editor != null) { editorElement = editor.ResolveEditor(propertyItem); } if (_propertyParent.EditorDefinitions != null) { // Priority #2: Custom Editors based on the name (same for all PropertyDescriptors). if (editorElement == null) { editorElement = CreateCustomEditor(_propertyParent.EditorDefinitions[GetPropertyName()]); } // Priority #3: Custom Editors based on the type (same for all PropertyDescriptors). if (editorElement == null) { editorElement = CreateCustomEditor(_propertyParent.EditorDefinitions[GetPropertyType()]); } } if (editorElement == null) { // Priority #4: Default Editor. Read-only properties use a TextBox. if (IsPropertyDescriptorReadOnly()) { editor = new TextBlockEditor(); } // Fallback: Use a default type editor. if (editor == null) { editor = CreateDefaultEditor(); } Debug.Assert(editor != null); editorElement = editor.ResolveEditor(propertyItem); } return(editorElement); }
internal static FrameworkElement CreateDefaultEditor(PropertyItem propertyItem) { ITypeEditor editor = null; if (propertyItem.IsReadOnly) { editor = new TextBlockEditor(); } else if (propertyItem.PropertyType == typeof(bool) || propertyItem.PropertyType == typeof(bool?)) { editor = new CheckBoxEditor(); } else if (propertyItem.PropertyType == typeof(decimal) || propertyItem.PropertyType == typeof(decimal?)) { editor = new DecimalUpDownEditor(); } else if (propertyItem.PropertyType == typeof(double) || propertyItem.PropertyType == typeof(double?)) { editor = new DoubleUpDownEditor(); } else if (propertyItem.PropertyType == typeof(int) || propertyItem.PropertyType == typeof(int?)) { editor = new IntegerUpDownEditor(); } else if (propertyItem.PropertyType == typeof(DateTime) || propertyItem.PropertyType == typeof(DateTime?)) { editor = new DateTimeUpDownEditor(); } else if ((propertyItem.PropertyType == typeof(Color))) { editor = new ColorEditor(); } else if (propertyItem.PropertyType.IsEnum) { editor = new EnumComboBoxEditor(); } else if (propertyItem.PropertyType == typeof(TimeSpan)) { editor = new TimeSpanEditor(); } else if (propertyItem.PropertyType == typeof(FontFamily) || propertyItem.PropertyType == typeof(FontWeight) || propertyItem.PropertyType == typeof(FontStyle) || propertyItem.PropertyType == typeof(FontStretch)) { editor = new FontComboBoxEditor(); } else if (propertyItem.PropertyType.IsGenericType) { if (propertyItem.PropertyType.GetInterface("IList") != null) { var t = propertyItem.PropertyType.GetGenericArguments()[0]; if (!t.IsPrimitive && !t.Equals(typeof(String))) { editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.CollectionEditor(); } else { editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.PrimitiveTypeCollectionEditor(); } } else { editor = new TextBlockEditor(); } } else { editor = new TextBoxEditor(); } return(editor.ResolveEditor(propertyItem)); }
internal static FrameworkElement CreateDefaultEditor(PropertyItem propertyItem) { ITypeEditor editor = null; if (propertyItem.IsReadOnly) { editor = new TextBlockEditor(); } else if (propertyItem.PropertyType == typeof(bool) || propertyItem.PropertyType == typeof(bool?)) { editor = new CheckBoxEditor(); } else if (propertyItem.PropertyType == typeof(decimal) || propertyItem.PropertyType == typeof(decimal? )) { editor = new DecimalUpDownEditor(); } else if (propertyItem.PropertyType == typeof(double) || propertyItem.PropertyType == typeof(double?)) { editor = new DoubleUpDownEditor(); } else if (propertyItem.PropertyType == typeof(int) || propertyItem.PropertyType == typeof(int?)) { editor = new IntegerUpDownEditor(); } else if (propertyItem.PropertyType == typeof(DateTime) || propertyItem.PropertyType == typeof(DateTime? )) { editor = new DateTimeUpDownEditor(); } else if ((propertyItem.PropertyType == typeof(Color))) { editor = new ColorEditor(); } else if (propertyItem.PropertyType.IsEnum) { editor = new EnumComboBoxEditor(); } else if (propertyItem.PropertyType == typeof(TimeSpan)) { editor = new TimeSpanEditor(); } else if (propertyItem.PropertyType == typeof(FontFamily) || propertyItem.PropertyType == typeof(FontWeight) || propertyItem.PropertyType == typeof(FontStyle) || propertyItem.PropertyType == typeof(FontStretch)) { editor = new FontComboBoxEditor(); } else { Type listType = CollectionEditor.GetListItemType(propertyItem.PropertyType); if (listType != null) { if (!listType.IsPrimitive && !listType.Equals(typeof(String))) { editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.CollectionEditor(); } else { editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.PrimitiveTypeCollectionEditor(); } } else { editor = new TextBoxEditor(); } } return(editor.ResolveEditor(propertyItem)); }
private PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance, PropertyGrid grid) { PropertyItem propertyItem = new PropertyItem(instance, property, grid); var binding = new Binding(property.Name) { Source = instance, ValidatesOnExceptions = true, ValidatesOnDataErrors = true, Mode = propertyItem.IsWriteable ? BindingMode.TwoWay : BindingMode.OneWay }; if (instance is ICustomTypeDescriptor) { binding.Source = (instance as ICustomTypeDescriptor).GetPropertyOwner(property); } propertyItem.SetBinding(PropertyItem.ValueProperty, binding); ITypeEditor editor = null; foreach (Attribute attr in propertyItem.PropertyDescriptor.Attributes) { if (attr is EditorAttribute) { editor = Activator.CreateInstance(Type.GetType((attr as EditorAttribute).EditorTypeName)) as ITypeEditor; if (editor != null) { editor.Attach(propertyItem); propertyItem.Editor = editor.ResolveEditor(); if (property is CustomizePropertyDescriptor) { propertyItem.Editor.IsEnabled = (property as CustomizePropertyDescriptor).IsEnabled; } return(propertyItem); } } } //check for custom editor if (CustomTypeEditors.Count > 0) { //first check if the custom editor is type based ICustomTypeEditor customEditor = CustomTypeEditors[propertyItem.PropertyType]; if (customEditor == null) { //must be property based customEditor = CustomTypeEditors[propertyItem.Name]; } if (customEditor != null) { editor = customEditor.Editor; } } try { //no custom editor found if (editor == null) { if (propertyItem.IsReadOnly) { editor = new TextBlockEditor(); } else if (propertyItem.PropertyType == typeof(bool) || propertyItem.PropertyType == typeof(bool?)) { editor = new CheckBoxEditor(); } else if (propertyItem.PropertyType == typeof(decimal) || propertyItem.PropertyType == typeof(decimal?)) { editor = new DecimalUpDownEditor(); } else if (propertyItem.PropertyType == typeof(double) || propertyItem.PropertyType == typeof(double?)) { editor = new DoubleUpDownEditor(); } else if (propertyItem.PropertyType == typeof(int) || propertyItem.PropertyType == typeof(int?)) { editor = new IntegerUpDownEditor(); } else if (propertyItem.PropertyType == typeof(DateTime) || propertyItem.PropertyType == typeof(DateTime?)) { editor = new DateTimeUpDownEditor(); } else if ((propertyItem.PropertyType == typeof(Color))) { editor = new ColorEditor(); } else if (propertyItem.PropertyType.IsEnum) { editor = new EnumEditor(); } else if (propertyItem.PropertyType == typeof(FontFamily) || propertyItem.PropertyType == typeof(FontWeight) || propertyItem.PropertyType == typeof(FontStyle) || propertyItem.PropertyType == typeof(FontStretch)) { editor = new FontComboBoxEditor(); } else if (propertyItem.PropertyType.IsGenericType) { if (propertyItem.PropertyType.GetInterface("IList") != null) { var t = propertyItem.PropertyType.GetGenericArguments()[0]; if (!t.IsPrimitive && !t.Equals(typeof(String))) { editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.CollectionEditor(); } else { editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.PrimitiveTypeCollectionEditor(); } } else { editor = new TextBlockEditor(); } } else { editor = new TextBoxEditor(); } } } catch (Exception ex) { //TODO: handle this some how } editor.Attach(propertyItem); propertyItem.Editor = editor.ResolveEditor(); if (property is CustomizePropertyDescriptor) { propertyItem.Editor.IsEnabled = (property as CustomizePropertyDescriptor).IsEnabled; } return(propertyItem); }